Tepu
Tepu

Reputation: 1702

Search engine friendly URL routing

My mvc3 app has the following URL: http://mysite.com/controller/details?id=56

I want to make it search engine friendly like

http://mysite.com/controller/title-of-the-entity-56

How can I achieve it?

Upvotes: 0

Views: 909

Answers (2)

Tepu
Tepu

Reputation: 1702

thanks Simon Whitehead that's exactly what i was looking for.

the below post describes in detail

http://www.deliveron.com/blog/post/SEO-Friendly-Routes-with-ASPnet-MVC.aspx

Upvotes: 0

Simon Whitehead
Simon Whitehead

Reputation: 65079

You need to setup a new route, however I would strongly advise you to re-think parsing the ID of the entity yourself. What I mean by that is:

With this URL, id can be passed to the action as a numeric data type (int, for example): http://mysite.com/controller/details?id=56

With this URL, the id can only be passed as part of a string: http://mysite.com/controller/title-of-the-entity-56

..which means you'll have to parse the string, extract the number and convert it.

It's better to aim for this format: http://mysite.com/controller/details/id/title-of-entity, somewhat how StackOverflow does it.

Try adding this route, above your default route, in Global.asax.cs:

routes.MapRoute(
            "DetailsRoute",
            "details/{id}/{entityTitle}",
            new { controller = "details", action = "Index", id = UrlParameter.Optional }
        );

Then you can have this as your index action in the details controller:

public ActionResult Index(int id, string entityTitle) {

}

Upvotes: 1

Related Questions