3

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.

Curtis Buys
  • 2,292
  • 1
  • 17
  • 13
halfbit
  • 54,462
  • 46
  • 195
  • 426
  • Incidentally you can also pass null for the last parameter in your ActionLink call there. To ensure correct overload resolution pass it as (object)null though – Andras Zoltan Feb 01 '11 at 07:54

1 Answers1

3

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 ;)

Andras Zoltan
  • 40,853
  • 11
  • 98
  • 156
  • got it. I typo'd my global.asa... this is the right answer. When you (or someone gets a chance I'd like to know if/when I should use routevalue dictionary and how to... – halfbit Jan 31 '11 at 22:36
  • `RouteValueDictionary` is used in some situations where you already have values in dictionary form that you can't then turn into an anonymous type because you don't know the contents. It's also the only way to merge route values in a generic way (for example, add a page number to the current url which might have an unknown number of other route values) - which is what MVC is doing all over the place as it produces links from the current request. In general I only use RouteValueDictionary when I have to either of these two because the anonymous type method is more terse. – Andras Zoltan Jan 31 '11 at 23:06