Can we use just the controller name and id parameter in URL for Asp.net MVC application

353 Views Asked by At

Suppose, I have a controller named category with an action method, Index which takes id as parameter.

Therefore, the URL appears like this : category/Index/foo. As you can see, the Index segment just doesn't seem right. A URL such as this : category/foo will be more readable and understandable.

Just like in SO, these guys use : question/857344

How can I achieve such a URL. In my routes, I have set defaults for all three : controller, action and id. But, when i try to visit category/foo, I get the "404 - resource not find"

1

There are 1 best solutions below

4
On BEST ANSWER
routes.MapRoute(
               "Category/{id}",
               new { controller = "Category", action = "Index", id = UrlParameter.Optional } // Parameter defaults
               );

You map it how you want it to look then specify the Action.

Also make sure you put it before the default route