Sunday, May 21, 2017

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

Leave a Comment

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);         } 

2 Answers

Answers 1

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.

Answers 2

I don't think there is a simple way. You want an instance of HttpRequestMessage class which sematically represents a (current) request to WebAPI. But you are not inside WebAPI and don't handle any WebAPI requests. Thus it is logical that you can't easily have a valid instance of HttpRequestMessage (if you could, what URL would it point to?). IMHO the most obvious way to work this around is to use RegisterCacheOutputProvider method from CacheOutputConfiguration to inject your own cache provider that would return an instance of IApiOutputCache that you can directly access using other means (such as globally visible singleton). It looks there is only one standard implementation of IApiOutputCache: MemoryCacheDefault. So it looks like if you return it from your registered provider, you'll be OK.

If you want to be a more hacky, it looks like all MemoryCacheDefault instances internally use the same shared (static) field to do the actual work so you probably can just create new MemoryCacheDefault in your filter or controller and still be OK for now, but to me this sounds way to hacky comparing to the alternative from the first part of my answer.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment