mpen
mpen

Reputation: 282885

Get route name from inside view?

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

Answers (3)

Raj
Raj

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

Tohid
Tohid

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

devlife
devlife

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

Related Questions