Create a route restriction that only applies the route when the action has a specific action filter
I have a list of actions on different controllers that are "Admin" functions (create, update, delete), but other actions on the same controllers that are not admin actions.
What I want to do is create a route that will be prefixed /Admin/
in front of all urls that invoke an action with a filter attribute Authorize
.
Is it possible to do this?
a source to share
Yes, anything is possible, but I think what you mean is, is it easy to do? And the answer is no. What you need to do is create your own route and then add that custom route to the route mapping. It's not easy, but the problem is that the routes are initialized before the controller, so you'll have to handle lookup and self reflection to validate your criteria.
There is an alternative option, you can try using ActionMethodSelectorAttribute, which allows you to create custom selectors for your Action Methods and ignore those that do not contain the Authorize attribute. An example of this attribute is ActionVerbAttribute.
The easiest way is to just create a custom extension for Html.ActionLink that does its own validation and saves it as soon as displayed, then creates duplicate routes for the same controller in your Global.asax.
a source to share