Reputation: 2935
In the MVC default route
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
whenever the index action is accessed, the url is not showing the action name when the "Index" action is accessed.
I would like to get that behaviour on a different controller, which is retrieving data for a single item. I would like the default action to be named "get" and for this action I would like the url not to display the action name, just the controller and the item id.
I thought that a similar route to the one above, like this:
routes.MapRoute(
"item_details",
"item/{action}/{id}",
new { controller = "item", action = "Get" }
);
would do the trick, but it gives me a url like localhost:xxxx/item/Get/152... What am I missing?
Upvotes: 12
Views: 19363
Reputation: 4627
For the users coming here as in ASP.NET MVC 5.2 days, it works when
you have id = UrlParameter.Optional
added, at least for my case
routes.MapRoute(
name: "item_details",
url: "item/{id}",
defaults: new { controller = "item", action = "Get", id = UrlParameter.Optional }
);
And if there are more routes in RouteConfig
class, then move this to the top of other routes
or
You can also do:
routes.MapRoute(
name: "item_details",
url: "item",
defaults: new { controller = "item", action = "Get", id = UrlParameter.Optional }
);
Ref: this SO answer
Upvotes: 0
Reputation: 448
Try this:
routes.MapRoute(
"Default", // Route name
"{controller}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Upvotes: 0
Reputation: 4701
Try this:
routes.MapRoute(
"item_details",
"item/{id}",
new { controller = "item", action = "Get" }
);
Upvotes: 16