In MVC 5 I you can access HttpContext
using HttpContext.Current
. What is the preferred way of accessing the HttpContext
or better, only the current RouteContext
?
1 Answers
RouteContext
is not an object you typically want to access. It is used by MVC to signal if the route was handled, and thus does not flow. You probably want to access RouteData
instead.
Here as a few ways to access it:
On a controller you can access - this.ActionContext.RouteData
or for the HttpContext.Current
equivalent this.ActionContext.HttpContext
or directly this.HttpContext
in an ActionFilter
you can access them through the method parameter:
public void OnActionExecuting(ActionExecutingContext context)
{
var routeData = context.RouteData;
var httpContext = context.HttpContext;
...
}
Anywhere else where you have access to the DI system (say constructor of a service, or when you have direct access to the service provider) you can get at the current request's ActionContext
but note that this works only when you are within a scope of the request and your serviceprovider you got passed in is scoped to the request.
public MyService(IScopedInstance<ActionContext> contextAccessor)
{
_httpContext = contextAccessor.Value.HttpContext;
_routeData = contextAccessor.Value.RouteData;
}
Note: You can also just write your own "Accessor" if you'd like it is just a simple class with a get/set property that gets registered as a Scoped service.

- 8,791
- 2
- 32
- 41
-
Ok, I see. In my case, the instance of `MyService` is created in `Startup.cs` like this: `app.UseMvc(routes => { routes.Routes.Add(new DefaultRouter(routes.DefaultHandler,new DefaultRouteResolver(new RouteResolverTrie(DocumentStore, // I need to put the IContextAccessor here //))));` Is it possible to use a `Func
>` at this point so I can send the current `IContextAccessor` for each request? – marcus Sep 11 '14 at 18:11 -
When you new something up, you are opting out of DI. So instead what you can do is to get the current service provider from the HttpContext passed into your route, through context.RequestServices. But note that action context is not available yet during routing, it's determined only after the action selection run and picked the action (which happens after routing). – Yishai Galatzer Sep 11 '14 at 20:19
-
It might be interesting to understand what you are actually trying to achieve, there might be a whole different question at hand here. – Yishai Galatzer Sep 11 '14 at 20:22
-
There isn't much more to tell, I have a class `RouteResolverTrie` which has a dependency to the `RouteData` object and the instance is created in `Startup.cs` so I need to inject the current `RouteContext` into the constructor. I'm not sure that this is the right thing to do, maybe I should just have `RouteContext` as a method parameter instead of a constructor parameter? You can se an example of what I mean here: http://codereview.stackexchange.com/questions/61953/routecontext-as-constructor-parameter-or-method-parameter-in-asp-net-vnext – marcus Sep 12 '14 at 13:14
-
1That's just not the right flow. Your route has no access to that at startup, nor it can be injected. Follow the code in template route – Yishai Galatzer Sep 13 '14 at 04:01
-
1In newer versions, replace IContextAccessor
with IScopedInstance – Malgaur Apr 01 '15 at 15:27. -
1in RC1, you can request `IActionContextAccessor` from DI – Dave Thieben Mar 22 '16 at 19:12