From inside a mvc (2) user control, I want to loop through all the route values.
So if I have controllers like:
UserController
AccountController
I need a collection of the values that will appear in the url like:
/user/...
/account/...
i.e. the values user, account.
How can I get this?
I tried RouteTables but couldn’t figure it out.
Oh, really a good question to keep my self busy for an hour.
To achieve the required functionality , we need to hook into MVC source and little bit of reflections.
By default Route names are not available , so we need to write a Route collection extension to save Route Name in RouteData tokens.
Modify the global.asax maproute call to invoke previous extension
Modified the MVC PathHelper a little bit.(Include this helper in the project)
Add few Helper methods in controller
Now we can write some reflection logics to read controllers, actions and routenames.
Points to remember :If in the route you have defined any default parameters, then url for those controller and action will be empty. e.g. in above example "/Home/Index" will be shown as "/"
Download the sample application Link To Download