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
}
1

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

But that means I have to have a custom attribute route attribute?
After poking around the source code, I realized that the route name mapping is saved in a private field in RouteCollection class private Dictionary<string, RouteBase> _namedMap = new Dictionary<string, RouteBase>(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?
From what I have read so far, It seems to be the least painful way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.