ASP.NET MVC 2: Route order is really important

by Christoph Keller 25. January 2011 13:49
If you have multiple MVC 2 Routes, check carefully the route order! If a less specific rule comes first, it will be threated first. For an example, I have a Language-Navigation Route: routes.MapRoute( "Language_Switch", "language/{language}", new { controller = "Language", action = "Index" } ); and the default Route: routes.MapRoute( "Default", "{controller}/{action}/{id}", new {controller = "Navigation", action = "Index", id = UrlParameter.Optional} ); If the default route would come first, it would threat a specific language route as default too. This affects the method Html.ActionLink() too, because it would resolve the url according the known Routes! So just be careful! :)
DotnetKicks
DotNetShoutout

Tags: , , ,

ASP.NET | C# | MVC 2

ASP.NET MVC 2: Use Html.ActionLink and add route values / HTML attributes to it

by Christoph Keller 11. January 2011 16:27
If you render a Link to another Controller / View, you can just use the method 'Html.ActionLink' on a view: Html.ActionLink("Link Title", "TargetAction", "TargetController"); Now, if you want to submit additional parameters / action parameters in the link, you can use the overloads of 'Html.ActionLink': Html.ActionLink("Link Title", "TargetAction", "TargetController", new { id = 1 }, new { title = "My link to TargetController, class = "linkClass" }) As you can see, the action 'TargetAction' will receive a parameter 'id' with the value of '1'. To the 'a href' HTML tag, a title and the CSS class "linkClass" will be rendered. According the MSDN (http://msdn.microsoft.com/en-US/library/dd504972.aspx) the key/value pairs will be retreived through reflection by examining the properties of the object.
DotnetKicks
DotNetShoutout

Tags: , ,

ASP.NET | MVC 2