Reputation: 282885
Using Attribute Routing you can define a route name. Is there a way I can get the name of the used route from inside my view?
Upvotes: 4
Views: 2329
Reputation: 19
MapMvcAttributeRoutes
has multiple overloads which expects a parameter for IDirectRouteProvider
implementation. This interface is responsible for generating routes. The default implementation for this interface is DefaultDirectRouteProvider
. Create a custom class that inherits from DefaultDirectRouteProvider
and override required methods like GetActionDirectRoutes
or GetControllerDirectRoutes
. In this method you will get RouteEntry
which contains the route name. You can add this name to datatokens.
Upvotes: 1
Reputation: 6679
According to this answer, you can use:
@{ var x = ViewContext.RouteData.Values["Action"]; }
to get route data. Then according to the right answer of this post: "How to get a custom attribute from object instance in C#", you can pull out the attributes.
Upvotes: 2
Reputation: 16145
This doesn't sound like a real good idea. You should probably add the route name to view data or the view model instead.
Upvotes: 1