Search code examples
asp.net-mvcroutesasp.net-mvc-5attributerouting

How to get route name from Attribute routing in ASP MVC5


Does anyone know how to get the route name from Attribute routing in a action filter?

For example I have a controller and attribute route like this:

[HttpGet]
[CustomActionAttribute]
[Route("~/index", Name="IndexPage")]
public async Task<ActionResult> Index()
{
    //Controller logic
}

Is it possible to get route name in the CustomActionAttribute?

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    //Get the current route name here
}

Solution

  • You can extend RouteCollection to achieve this. You can find example code for this here