9

I'm trying to do something like stackoverflow

Take a link from stackoverflow for example:

Hidden Features of C#?

if you remove the last part (Hidden Features of C#?) it still returns the same result.

For my routing in Global.asax I tried doing something like "{action}/{id}/{title}"

On my page, this is my link:

<%= Html.ActionLink(video.Title, "Details", "Videos", new {id = video.ID, title = video.Title.Replace(" ", "-")}, null) %>

This does what I want it to do for the most part except that after the id it throws in "?title=blah-blah-blah"

I want it to say "id/blah-blah-blah"

What's my problem? (Besides being a noob)

2
  • Could you post all the routes in your global.asax.cs? Maybe another one is being matched first ... Commented Sep 1, 2010 at 16:29
  • @Peter indeed that was the case Commented Sep 1, 2010 at 16:47

2 Answers 2

9

I wrote a blog post on this a while back and thought it might be helpful:

http://web.archive.org/web/20170416234816/http://mynameiscoffey.com/2010/12/19/seo-friendly-urls-in-asp-net-mvc/

Basically you need to check in your action for the presence of the correct SEO-Friendly title when your action is executing, and if it doesn't find it, issue a redirect back to the browser to the correct SEO-Friendly URL.

Sign up to request clarification or add additional context in comments.

4 Comments

Great post. May I suggest putting .Trim('-') at the end of your SeoName method to stop things like "One (Two)" becoming "one-two-".
perfect! specially for me migrating from webform to mvc.
one addition: SeoName(string name) fails working with unicode strings such as درباره ما without explicit urlencoding. ie. HttpContext.Current.Server.UrlEncode(Regex.Replace(input.ToLower().Replace(@"'", String.Empty), @"[^\w]+", "-")); I don't know the reason. without encoding, it throws 404 exception.
Updated to an Internet Archive version of the link.
5

That route looks like it should work with that call to ActionLink, so this is a bit of a guess. Are you registering your {action}/{id}/{title} route after the default route? If so, the default route will match first, and just put the title value in the querystring since it doesn't appear in the path. If you register your custom route before the {controller}/{action}/{id} default, it should work.

1 Comment

It is registered before the default route, however, you did point me in the right direction. It actually did come down to the actual sequence of the registered routes. thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.