Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Access to RouteData/ControllerContext/ActionContext #3826

Closed
sporty81 opened this issue Dec 29, 2015 · 16 comments
Closed

Access to RouteData/ControllerContext/ActionContext #3826

sporty81 opened this issue Dec 29, 2015 · 16 comments

Comments

@sporty81
Copy link

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.

    var wrapper = new HttpContextWrapper(context);
    var routeData = RouteTable.Routes.GetRouteData(wrapper);

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...

@rynowak
Copy link
Member

rynowak commented Dec 29, 2015

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?

@sporty81
Copy link
Author

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.

var contextAccessor = context.RequestServices.GetService(typeof (IActionContextAccessor)) as IActionContextAccessor;

@sporty81
Copy link
Author

It is bizarre to me that this works in my controller.

    [HttpGet]
    public IEnumerable<string> Get()
    {
        var service = this.HttpContext.RequestServices.GetService(typeof(IActionContextAccessor)) as IActionContextAccessor;
        //service.ActionContext is not null here
        return new string[] { "value1", "value2" };
    }

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.

@rynowak
Copy link
Member

rynowak commented Dec 29, 2015

We actually have a feature we add to HttpContext.Features now in routing that would work 'on the way out'. This really isn't what it's intended for, but it seems like it would do what you want for access to route data in most configurations.

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
Copy link
Member

rynowak commented Dec 29, 2015

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().

IActionContextAccessor provides access to the current action. Once you exit MVC there's no current action. If you want to change that behavior, you could implement a custom IActionContextAccessor that does whatever you want.

@sporty81
Copy link
Author

@rynowak That feature change for IRoutingFeature should do the trick. Looks like that is for the next release and not in rc1?

@rynowak
Copy link
Member

rynowak commented Dec 29, 2015

Correct

@Eilon
Copy link
Member

Eilon commented Dec 29, 2015

@rynowak will this just work in RC2 and we can close this? Or is there more work?

@mikes-gh
Copy link

Would this help? Came across this issue yesterday and missed it at first.

public class SomeClass
{
    IHttpContextAccessor _httpContextAccessor;

    public SomeClass(IHttpContextAccessor httpContextAccessor)
    {
        _httpContextAccessor = httpContextAccessor;            
    }

    public string GetRouteValue(string routeParameter)
    {
        return _httpContextAccessor.HttpContext.GetRouteValue(routeParameter);
    }
}

PS. routeParameter could be "controller" or "action" to get those values

@Eilon
Copy link
Member

Eilon commented Jan 15, 2016

@mikes-gh yes that could work.

@Eilon
Copy link
Member

Eilon commented Jan 15, 2016

I don't think there's any further action required on this issue, so closing.

@Eilon Eilon closed this as completed Jan 15, 2016
@sporty81
Copy link
Author

@mikes-gh I can't find a GetRouteValue method anywhere on a DefaultHttpContext object?

@mikes-gh
Copy link

That's working code can you post yours?
I am using rc2 aspvnext packages btw

@sporty81
Copy link
Author

@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

@mikes-gh
Copy link

@sporty81

https://github.com/aspnet/Routing/blob/dev/src/Microsoft.AspNetCore.Routing.Abstractions/RoutingHttpContextExtensions.cs

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

@mikes-gh
Copy link

Try adding
using Microsoft.AspNet.Routing

Note namespace will soon change but should work with aspvnext next feed

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants