Why is RouteLink generating a friendly URL but ActionLink 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 strange part of {someEnum} is that I use a common controller for all enumeration values ​​that form the typical part of a URL controller. For example, / EnumVal1 / Action / and / EnumVal2 / Action / use the same controller. However, this is not part of the problem.

Consider the following two binding methods:

 <%=Html.RouteLink("Click me","Routename1", new { id = 32, stringId = "Yatzy" })%> <%=Html.ActionLink("Click me", "SpecificAction", "EnumVal1", new { id = 32, stringId = "Yatsy" }, null)%> 

RouteLink generates the correct URL, which will be / EnumVal 1 / SpecificAction / 32 / Yatzy

Does ActionLink generate a URL that looks like / EnumVal 1 / SpecificAction / 32? stringId = Yatzy

Why is this? Can someone explain this to me please.

+7
c # asp.net-mvc routes actionlink
source share
1 answer

RouteLink can use only one route specified by you . ActionLink will use the first suitable route, regardless of which one you planned or not. Your two examples probably correspond to different routes.

Debugging routing Phil Haack will help clarify this.

+4
source share

All Articles