0

I have a MVC 5 project with model named ProductModel and controller named ProductController. I also have a Product data row with field Title: Canon DSLR Cameras By default, when users want to view details product, they will see the url http://../Product/Details/1.

Can we change the url to: http://../Product/Canon-DSLR-Cameras ?

Thanks.

Mr Stone
  • 35
  • 2
  • 8

1 Answers1

2

In your RouteConfig.cs

You can simple add route map like:

routes.MapRoute("Details", "Product/{customUrl}",
       new { controller = "Product", action = "Details" },
       new { customUrl = @"\S+" } );

And write a controller action (ProductController.cs):

public ActionResult Details(string customUrl)
{
   // find ProductModel by customUrl
   ...
   return View(); // pass the model matching customUrl
}
jonny.novikov
  • 126
  • 1
  • 5
  • Thanks. It works for me. In other case, my site will used in English and Spanish. So, can we change the link to http://../producto/. while continuing use ProductController ? – Mr Stone Apr 19 '16 at 09:48
  • @MrStone Please refer answer to this question http://stackoverflow.com/questions/4271769/how-to-localize-the-controller-names-and-actions-in-an-asp-net-mvc-application – jonny.novikov Apr 19 '16 at 11:20