Is it possible in MVC to do the following with a single controller "ListController" to take care of the following pages...
www.example.com/List/Cars/ForSale/{id} optional
www.example.com/List/Cars/ForRent/{id} optional
www.example.com/List/Search/
www.example.com/List/Boats/ForSale/{id} optional
www.example.com/List/Boats/ForRent/{id} optional
www.example.com/List/Boats/Search/
If not, is there any way to get around it besides making a CarsController and BoatsController separate? They will be using the same logic just would like the URLs different.
You can definitely do this. It is simple using routing. You can route the different urls to different actions in your controller.
Here are examples of defining some of the above urls:
routes.MapRoute("CarSale"
"/List/Cars/ForSale/{id}",
new { controller = "list", action = "carsale", id = UrlParameter.Optional } );
routes.MapRoute("ListSearch"
"/List/search",
new { controller = "list", action = "search"} );
routes.MapRoute("BoatSale"
"/List/Boats/ForSale/{id}",
new { controller = "list", action = "boatsale", id = UrlParameter.Optional } );
Then in your controller you would have action methods for each:
public ListController
{
// ... other stuff
public ActionResult CarSale(int? id)
{
// do stuff
return View("CarView");
}
public ActionResult BoatSale(int? id)
{
// do stuff
return View("BoatView");
}
// ... other stuff
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With