I have an MVC app and to populate the routes table there’s a method like this:
public static void RegisterRoutes(RouteCollection routes){
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
....
routes.MapRoute("default", "{controller}/{action}/{id}", new { controller = "Home", action = "index", id="" });
... etc
}
//and in global.asax it gets called like:
RegisterRoutes(RouteTable.Routes);
Now if I call that method from outside the MVC app (in some console app, just to get routes), I would pass a new empty collection, it will be populated with the routes used in the MVC app, but what’s use of it?
Can I based only on collection of routes, recognize the url for given controller name, action and other parameters? (again, from the outside of the web app)
I think routes[0].GetVirtualPath() is what you’re looking for. However, you’ll have to pass a RequestContext and RouteValueDictionary to it. If a route is not able to handle your input, it will return null