2

I have a route established like the return a particular blog entry when accessed It looks like this

www.mywebsite.com/100

However I want that when returning the page I also add the blog title to the URL For e.g. user may enter the above url but when he gets the page back the url must look like this:

www.mywebsite.com/100/My-Blog-Title

Just like it happens here on Stakoverflow that: I enter this URL

http://stackoverflow.com/questions/11227809/

And after the request is received the URL looks like this:

http://stackoverflow.com/questions/11227809/why-is-processing-a-sorted-array-faster-than-an-unsorted-array

How can achieve this in my MVC project? Can anyone please help.

Please also comment over the SEO friendliness while suggesting your preffered approach.

Thankyou

Maven
  • 14,587
  • 42
  • 113
  • 174
  • 1
    Take a look at this link: http://stackoverflow.com/questions/25259/how-does-stack-overflow-generate-its-seo-friendly-urls/25486#25486 – Lin Jan 08 '14 at 20:35

3 Answers3

0

It seems to be a simple redirection. See http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.redirecttoaction(v=vs.108).aspx

cubitouch
  • 1,929
  • 15
  • 28
0

You can use JavaScript for it.

Return the blog title stored in a JavaScript variable somewhere.

var blogTitle = "My-Blog-Title"

Read the blog title and read this link to change the url without redirecting.

Yogesh
  • 2,198
  • 1
  • 19
  • 28
0

If you open the Network tab in the Chrome Developer tools and load up the address (id only) to this question (http://stackoverflow.com/questions/21005768/), you'll see that you get back a an HTTP 301 response - a permanent redirect to the friendly url (http://stackoverflow.com/questions/21005768/add-string-to-the-requested-url/).

To respond with this in ASP.NET MVC you would do something like the following:

public ActionResult GetThingy(int id)
{
    var title = GetTitle(id);
    return RedirectPermanent(Url.Action("GetThingy", new { id = id, title = title} );
}

that would redirect to another action on the controller like:

public ActionResult GetThingy(int id, string title)
{
    //do your thing here
}
Simon C
  • 9,458
  • 3
  • 36
  • 55