Why does RouteLink generate friendly URL while ActionLink does not?
I have a question regarding RouteLink vs. ActionLink.
Consider the following route
routes.MapRoute("Routename1",
"{someEnum}/SpecificAction/{id}/{stringId}",
new { controller = "MyController", id = (int?)null, stringId= (string)null, action = "SpecificAction" },
new { someEnum= "(EnumVal1|EnumVal2)" }
);
The weird {someEnum} part is because I use a general controller for all values of an enum that form the typical controller part of a url. For instance, /EnumVal1/Action/ and /EnumVal2/Action/ use the same controller. That's not part of the problem, however.
Consider the following two ways of linking:
<%=Html.RouteLink("Click me","Routename1", new { id = 32, stringId = "Yatzy" })%>
<%=Html.ActionLink("Click 开发者_运维知识库me", "SpecificAction", "EnumVal1", new { id = 32, stringId = "Yatsy" }, null)%>
The RouteLink generates the correct url, which would be /EnumVal1/SpecificAction/32/Yatzy
The ActionLink generates an url that looks like /EnumVal1/SpecificAction/32?stringId=Yatzy
Why is this? Could someone explain this to me, please.
RouteLink
can only ever use the one route you specify. ActionLink
will use the first matching route, whether it's the one you intended or not. Your two examples are probably matching different routes.
Phil Haack's routing debugger would help clarify this.
精彩评论