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.

2
  • Have you tried anything? With routes maybe? Commented Apr 17, 2016 at 16:00
  • I have tried to find solutions with keywords like "MVC URL Rewrite". But they can not resolve. Commented Apr 17, 2016 at 16:07

1 Answer 1

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
}
Sign up to request clarification or add additional context in comments.

2 Comments

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 ?
@MrStone Please refer answer to this question stackoverflow.com/questions/4271769/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.