How to insert Route to the top of RouteCollection
I a开发者_开发知识库m need to insert a route during the mvc application processing. I am in stuck because i can use MapPageRoute only to add new route to the end of routings table, and i can use Insert to add route to the start of collection, but in this case i can't define this routing name, so i can't manage it in future.
So the question: is any opportunity to add route with defined name to the start of routes table exists?
Does anyone know?
p.s. map to the end and use Reverse is bad idea.
You can use a combination of Map and insert. Mapping a route returns a Route object. You can map a route, immediately remove it and then Insert it like so:
Route r = routes.MapRoute(
"SomeName", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.Remove(r);
routes.Insert(0, r);
This gets you a named route at the top of your Route Table.
精彩评论