0

So I have got a Views folder named Movies. Inside that I have Index,Create,Delete,Details,Edit. (Auto generated). Now when I click on the details button the URL becomes xx.com/Movies/Details/2. The number represents the ID from the database. Is there a way for it to show the Title instead of ID?

niko619
  • 433
  • 9
  • 20
  • [this question/answer](http://stackoverflow.com/questions/16248732/mvc-4-creating-slug-type-url) might help –  Oct 14 '14 at 02:13
  • Ah looks like I need to understand routes better then. How does Google indexing work when your modifying routes? – niko619 Oct 14 '14 at 02:50

1 Answers1

1

Yes of course. You just need to update the Route. In your Route Config file, you will notice a line like this :

 routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });

Here, you will need to change that to

  routes.MapRoute("Default", "{controller}/{action}/{title}", new { controller = "Home", action = "Index", title = UrlParameter.Optional });

Now, you should change your action method signature from

    public ActionResult Details(int id)
    {

        return View();
    }

to

    public ActionResult Details(string title)
    {

        return View();
    }

By the way, I did not test it, I have just written out of concept. You would need to test, specially if your title is not url encoded or contains spaces without dashes.

EDIT:

in MVC 5, you can use Attribute based routing.

I have used that to generate SEO URL like this:

[Route("Products/{slug}-{productId}")] 
public ActionResult Details(int productId, string slug)
{
   return View();
}

When I needed to create the url, I used snippet :

Url.Action("Details", "Products", new { productId = 201, slug = "My-Keyword-Title" })
Emran Hussain
  • 11,551
  • 5
  • 41
  • 48
  • I have tried this however I get this error `HTTP Error 400.0 - Bad Request`. Also the URL is still saying ID`http://localhost:5107/Tv/Details?id=4` – niko619 Oct 14 '14 at 04:19
  • Ah this is much better. How would I be able to put info from a database in the slug though? – niko619 Oct 14 '14 at 04:32
  • I have updated the Answer. Please check the example how I created the Url.Action. You will need to get your slug from your database and build the url. How ? same like you get any data from your database. IF you used the ID for your route, you would have got that id somehow from your database or Query string right ? you can query your database with the id to get your slug and then build the url. If you are still not clear, feel free to ask. – Emran Hussain Oct 14 '14 at 04:35
  • Would the item I'm trying to find have to be a primary key? – niko619 Oct 14 '14 at 04:44
  • URL routing has nothing to do with your database/primary key etc. Routing is for URL structure. Your id can be primary key or anything. How would you access your database to get your slug - completely depends on how you designed your database and that is completely beyond the context of your original question related to URL Routing. If you have any question related to Database, you may post a new question in StackOverflow and we can discuss there about your Database Question. By the way, if I could answer your question about Routing, please accept it as Answer. Thank you. – Emran Hussain Oct 14 '14 at 04:51