Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get HttpRequestMessage from ActionFilter or ASP.NET MVC Web Controller outside of Web API

I am having a tuff time trying to get an instace of a HttpRequestMessage so I can pass it to the method GetCacheOutputProvider below from an ActionFilter and/or normal ASP.NET MVC Controller. I know I can from the Web API, but what about these instances.

public class CacheResetFilter : ActionFilterAttribute
    {
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            var cache = GlobalConfiguration.Configuration.CacheOutputConfiguration().GetCacheOutputProvider(HTTPREQUESTMESSAGE);
                cache.Contains("eventid=" + eventId);

            base.OnActionExecuted(filterContext);
        }
like image 667
Mike Flynn Avatar asked Nov 08 '22 22:11

Mike Flynn


1 Answers

1.In a MVC Controller you can do like:

public class HomeController : Controller
{
   public ActionResult Test()
        {
            HttpRequestMessage httpRequestMessage =
                HttpContext.Items["MS_HttpRequestMessage"] as HttpRequestMessage;
            return View();        
        }
 }

2.In action filter you can do like :

public class HttpRequestMessageAttribute : System.Web.Mvc.ActionFilterAttribute
{
    public override void OnActionExecuted(System.Web.Mvc.ActionExecutedContext filterContext)
    {
        HttpRequestMessage httpRequestMessage =
            filterContext.HttpContext.Items["MS_HttpRequestMessage"] as HttpRequestMessage;
        //var cache = GlobalConfiguration.Configuration.CacheOutputConfiguration().GetCacheOutputProvider(httpRequestMessage);
        //cache.Contains("eventid=" + eventId);

        base.OnActionExecuted(filterContext);
    }
}

OR

    public class HttpRequestMessageAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            HttpRequestMessage httpRequestMessage =
                filterContext.HttpContext.Items["MS_HttpRequestMessage"] as HttpRequestMessage;

            base.OnActionExecuting(filterContext);
        }
   }

Hopefully it's help for you.

like image 183
Ashiquzzaman Avatar answered Nov 17 '22 00:11

Ashiquzzaman