Jamie
Jamie

Reputation: 379

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

Upvotes: 10

Views: 4540

Answers (1)

SLaks
SLaks

Reputation: 887877

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.

Upvotes: 15

Related Questions