Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove Index from MVC url with routeValue

How can I remove the Index from the MVC url that has a routeValue?

E.g. http://localhost/Beverage/Index/WhiteWine to http://localhost/Beverage/WhiteWine

but still be able to have http://localhost/Beverage/ShowBeverage/1

like image 505
Jamie Avatar asked May 12 '11 17:05

Jamie


1 Answers

You can create a custom route:

MapRoute("My Route Name",
         "Beverage/{id}",
         new { controller = "Beverage", action = "Index" });

Note that the controller name must be hard-coded in the route, then specified in the defaults to tell MVC which controller to use.
If you take the naive approach and map {controller}/{id}, it will accept any URL of the form a/b, which is not what you want.

like image 140
SLaks Avatar answered Oct 06 '22 15:10

SLaks