tags:

views:

38

answers:

2

I'm very new to MVC and I'm trying to get a new site set up using it. For SEO reasons we need to make the url of a page something like "Recruiter/4359/John_Smith" or basically {controller}/{id}/{name}. I have that working when I create the url in the code behind like so...

//r is a recruiter object that is part of the results for the view

r.Summary = searchResult.Summary + "... <a href=\"/Recruiter/" + r.Id + "/" + r.FirstName + "_" + r.LastName + "\">Read More</a>"

But when I am using the collection of results from a search in my view and iterating through them I am trying to create another link to the same page doing something like <%=Html.ActionLink<RecruiterController>(x => x.Detail((int)r.Id), r.RecruiterName)%> but that doesn't work. When I use that code in the view it gives me a url in the form of /Recruiter/Detail/4359 I was told by a coworker that I should use the Html.ActionLink to create the link in both the view and the controller so that if the route changes in the future it will automatically work. Unfortunately he wasn't sure how to do that in this case. So, my problems are...

  1. How can I make the Html.ActionLink work in the view to create a url like I need (like r.Summary above)?
  2. How do I use the Html.ActionLink in a controller instead of hardcoding the link like I have above?
A: 

It is a good idea to use the ActionLink method to write out links as your coworker says, that way they will always match your routes.

In your current case the reason it is writing out the method is because it is based on the default routing. You can fix this by adding another route above the default one in the Global.asax. You just need to stipulate the format you want like this:

routes.MapRoute(
    "Recruiter",
    "Recruiter/{id}/{name}",
    new { controller = "Recruiter", action = "Details" }
);

MVC will work through your routes in the order they are registered so putting this before the default will make it use your route instead.

EDIT:

You might find this route debugging tool useful.

Rob West
Thats basically what I have right now but it's not generating the url. And, if it uses the route to generate the url, how can I have it it replace the spaces in the recruitername with underscores?<code>routes.MapRoute("Recruiter", // Route name"Recruiter/{id}/{recruitername}", // URL with parametersnew { controller = "Recruiter", action = "Detail" }, // Parameter defaultsnew { id = @"\d{1,}" } // id parameter must be a number);</code>
geoff
Sorry, I don't know why it's not formatting my response correctly but there should be a new code paragraph in there. I'm new to stackoverflow and the FAQs say that the <code> tag is recognized so if someone knows what I'm doing wrong, let me know. Thanks.
geoff
Add a property to your recruiter class called something like UrlName that escapes characters and use that for your route, I've done this and it works fine
Rob West
A: 

I came across this blog post which got me going in the right direction.

http://www.chadmoran.com/blog/2009/4/23/optimizing-url-generation-in-aspnet-mvc-part-2.html

geoff