5

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
}
Wei
  • 422
  • 1
  • 7
  • 15
  • Possible duplicate of [How can I get the route name in controller in ASP.NET MVC?](https://stackoverflow.com/questions/363211/how-can-i-get-the-route-name-in-controller-in-asp-net-mvc) – Ian Kemp Jun 18 '19 at 11:48

1 Answers1

1

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

Community
  • 1
  • 1
Pankaj Bodani
  • 303
  • 2
  • 8
  • But that means I have to have a custom attribute route attribute? – Wei Oct 09 '14 at 20:32
  • 1
    After poking around the source code, I realized that the route name mapping is saved in a private field in RouteCollection class private Dictionary _namedMap = new Dictionary(StringComparer.OrdinalIgnoreCase); So I don't think we could access it. The only viable way would be create one for our own as you suggest it. Any other ideas? – Wei Oct 09 '14 at 21:26
  • From what I have read so far, It seems to be the least painful way. – Pankaj Bodani Oct 10 '14 at 02:52