I have a web site written in ASP.NET MVC 4.5
I have a controller called Products with views about each product.
I want with one of our products that when a user goes to http://www.website.com/ProductName redirects to http://www.website.com/Products/ProductName.
I am thinking right now this two approaches:
- Create a Controller called like
ProductNameand useRedirectToRoute() - Modify the
RouteConfig.csfile and add one.
What is your the best approach and why? Is there another?
I’m with @rjovic, definitely go with the second approach and add a new route.
The first approach would require creating a new controller for every product, which seems to go against good design principles; why have a separate class for multiple products that behave the same way? Routes are designed for exactly this kind of scenario: mapping a user-friendly URL scheme to the appropriate handlers in your application logic.
There are two possible approaches to designing the route in this case: