Access to RouteData/ControllerContext/ActionContext #3826
Comments
Routing is generally going to be the last middleware to run, and will invoke MVC itself, so trying to log the 'current route' from another middleware isn't a good fit for the typical configuration. Our built-in logging logs the route that's matched, as well as creates a scope for the MVC action that's selected. The hope is that users don't need to reimplement logging for standard framework concepts and can rely on what we've built in. Some background and examples: http://www.tugberkugurlu.com/archive/asp-net-5-and-log-correlation-by-request-id Is there a reason why what we have built in doesn't meet your needs? |
We (Stackify) are basically a custom serilog sink. I will look in to how the new logging works and how we will have to work with that. We also have appenders for NLog, log4net, etc. My second use case is trying to access the route data from middleware, outside of the logging use case. I need to do it at the end of the request. So after the MVC action has already been executed. I think there are a ton of uses cases where people would want to do this... but now they can't. If this worked from the middleware, like it does in the controller, it would solve my problem.
|
It is bizarre to me that this works in my controller.
But immediately after that it goes back to my middleware and the same code gets a null from the HttpContext it has a reference to from Invoke(). This makes no sense to me. |
We actually have a feature we add to https://github.com/aspnet/Routing/blob/dev/src/Microsoft.AspNet.Routing/RouterMiddleware.cs#L43 This won't help you with access to MVC assets. |
|
@rynowak That feature change for IRoutingFeature should do the trick. Looks like that is for the next release and not in rc1? |
Correct |
@rynowak will this just work in RC2 and we can close this? Or is there more work? |
Would this help? Came across this issue yesterday and missed it at first.
PS. routeParameter could be "controller" or "action" to get those values |
@mikes-gh yes that could work. |
I don't think there's any further action required on this issue, so closing. |
@mikes-gh I can't find a GetRouteValue method anywhere on a DefaultHttpContext object? |
That's working code can you post yours? |
@mikes-gh OK, I'm looking at the source code in the "dev" branch of this Mvc project and that method (GetRouteValue) doesn't exist. https://github.com/aspnet/Mvc/search?utf8=%E2%9C%93&q=GetRouteValue |
Because it's an extension I am not sure vs helps you with the using or dependency. Not at my desk now but should get you going |
Try adding Note namespace will soon change but should work with aspvnext next feed |
In previous versions of ASP.NET I could use the RouteData to get access to the controller and action names. *How do I do this in ASP.NET5? I have tried everything I can to no avail. *
My use case is from a logging appender trying to figure out the route being used. I would also like to access it at the end of middleware.
Before there was a couple ways to do it. This was one of them.
I have seen several people asking about this across stackoverflow and other sites but can't find any answers besides implementing routing middleware, which isn't an option for something like a logging appender.
Via the HttpContext object is there some way to figure out the route via ApplicationServices, RequestServices or Features? Can I get the current executing ActionContext or ControllerContext somehow?
There has to be a way to do this...
The text was updated successfully, but these errors were encountered: