I have a URL. Is it possible to somehow get the controller- and action-name and the routevalues? (which would normally be generated by the .NET MVC framework based on the definition in global.asax)
views:
44answers:
3Thanks for you highly motivated answer
Ropstah
2010-01-10 14:46:39
Well i assumed by "I have a URL" you weren't referring to your own site, in which case it is impossible. But i suppose that Alexander Taran's answer does make sense if thats what your after, and it's very useful for debugging complex routes.
Paul Creasey
2010-01-10 14:50:52
I suppose I had better ask - why do you want to know this? making decision based upon the names of controllers/actions/route-values is going to make you code less loosely coupled.
belugabob
2010-01-09 21:57:18
+2
A:
Where and when do you want to get the values?
From within your code you can call:
HttpContextBase context = new HttpContextWrapper(HttpContext.Current);
RouteData rd = RouteTable.Routes.GetRouteData(context);
if (rd != null) {
string controllerName = rd.GetRequiredString("controller");
string actionName = rd.GetRequiredString("action");
}
Please note that this only works from within your application's code. There is definitely no way to get this information about other sites and applications.
Eilon
2010-01-09 21:58:32