How do I use an ActionLink with the RouteValueDictionary?

halfbit picture halfbit · Jan 31, 2011 · Viewed 9.9k times · Source

I'm trying to make my URL look like this:

http://domain.com/controller/action/123/SomeTextForSEO

I tried using an Action Link, but that appends ?company=SomeTextForSEO instead of the company name after a slash.

 <%: Html.ActionLink("DomainList", "Index", "DomainList", new { id = item.CompanyID, company = item.CompanyDisplayName.Trim() }, new object { })%> 

and now I think I need to use a RouteValueDictionary but I'm unsure of how to do this in ASP.NET syntax. Can someone point me in the right direction? The MSDN examples are insufficient.

Answer

Andras Zoltan picture Andras Zoltan · Jan 31, 2011

If you go to global.asax.cs and add a new route similar to the existing default route with the pattern

"{controller}/{action}/{id}/{company}"

Before the default one, you should find that the link will generate correctly with your ActionLink call as-is.

I'm on a phone so I'd like to be more verbose (a route restriction would be a good idea to prevent this route interfering with the default), but the HTC keyboard is not code friendly ;)