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

1 Answer 1

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.

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

4 Comments

hi thanks for your answer but the code should run on routing natively.
yes when you redirect or you can set url of links with that code
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 :)
I am having the same problem and came to this link.. I think it is quite useful to have: deliveron.com/blog/post/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.