10

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

1 Answer 1

15

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.

Sign up to request clarification or add additional context in comments.

3 Comments

This doesn't work. ActionLinks don't update and if I remove the Index from the url it 404s. I am new to routes in MVC so I may be missing a step.
@Jamie: Put this before your other routes, or it will caught by the general route first.
Brilliant that was the step I missed

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.