In global.asax
routes.MapRoute(
"Test_Default", // Route name
"test/{controller}/{action}", // URL with parameters
new { }
);
routes.MapRoute(
"Default",
"{universe}",
new { controller = "notfound", action = "error"}
);
I have a controller: Home, containing an 开发者_StackOverflowaction: Index Enter the url in browser: h**p://localhost:53235/test/home/index
Inside the index.aspx view in <body> tag: I want to link to the second route.
<%=Html.RouteLink("Link", new { universe = "MyUniverse" })%>
Shouldn't this generate a link to the second route in Global.asax? The generated url from the above is: h**p://localhost:53235/test/home/index?universe=MyUniverse. I can only get it to work, if I specify the name of the route: <%=Html.RouteLink("Link", "default", new { universe = "MyUniverse" })%>
Am I missing something?
As you've discovered you need to use the route name if you want to generate a link to the second route. The first route will always be evaluated because even if the universe
parameter doesn't exist in the route definition it is just passed as query string argument.
精彩评论