3

Possible Duplicate:
How does StackOverflow generate its SEO-friendly URLs?

I use Asp.net 4 and Routing

I use this Route to create SEO friendly URLs for my website. Title url paramenter is a string example "This is a Title" as result I get the URL in browser with this format /Content/This%20is%20a%20Title.

I would rather have the white spaces %20 replaced with a more readable dash for example: /Content/This-is-a-Title.

Any idea how to do it? Thanks for your help on this

        routes.MapPageRoute(
            "View Content",                     // Route name
            "Content/{Title}",                  // Route URL
            "~/Cms/FrontEndCms/Content.aspx"    // Web page to handle route
        );
Community
  • 1
  • 1
GibboK
  • 64,078
  • 128
  • 380
  • 620

1 Answers1

4

When you redirect to this URL, use the following:

Response.Redirect(Page.GetRouteUrl(
    "View Content", 
    new { Title=(yourtitlehere).ToString().Trim().Replace(" ","-") })
);

I added Trim(). If the title starts or ends with a space the server may think it's a different path and return a 404: Resource not found.

George Stocker
  • 55,025
  • 29
  • 167
  • 231
  • 1
    hi thanks for your answer but the code should run on routing natively. – GibboK Oct 11 '11 at 05:49
  • yes when you redirect or you can set url of links with that code – Ahmed Samir Hasan Oct 11 '11 at 14:23
  • Thanks, now I understand. Your solution is quick to adopt, but I suppose could have some performance draw back. At the end I'm stripping my urls and storing in the DB, so I use routing to get the clean URL or my links. Anyway many thanks :) – GibboK Oct 14 '11 at 18:44
  • 1
    I am having the same problem and came to this link.. I think it is quite useful to have: http://www.deliveron.com/blog/post/SEO-Friendly-Routes-with-ASPnet-MVC.aspx – Subliminal Hash Apr 11 '12 at 04:35