12

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?

ArunPratap
  • 4,816
  • 7
  • 25
  • 43
yu_ominae
  • 2,975
  • 6
  • 39
  • 76

3 Answers3

15

Try this:

routes.MapRoute(
        "item_details",
        "item/{id}",
        new { controller = "item", action = "Get" }
        );
robasta
  • 4,621
  • 5
  • 35
  • 53
0

Try this:

routes.MapRoute(
            "Default", // Route name
            "{controller}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );
saurav singh
  • 438
  • 6
  • 16
0

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

Irf
  • 4,285
  • 3
  • 36
  • 49