Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Routing: How to hide action name in url?

Tags:

c#

asp.net-mvc

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?

like image 844
yu_ominae Avatar asked Jan 23 '12 08:01

yu_ominae


People also ask

What is a URL action?

A URL action is a hyperlink that points to a web page, file, or other web-based resource outside of Tableau. You can use URL actions to create an email or link to additional information about your data. To customize links based on your data, you can automatically enter field values as parameters in URLs.

How to set RouteConfig in MVC?

Each MVC application has default routing for the default HomeController. We can set custom routing for newly created controller. The RouteConfig. cs file is used to set routing for the application.

Where is RouteConfig in MVC?

Every MVC application must configure (register) at least one route configured by the MVC framework by default. You can register a route in RouteConfig class, which is in RouteConfig. cs under App_Start folder.

How to define route in MVC?

Routing in ASP.NET MVC cs file in App_Start Folder, You can define Routes in that file, By default route is: Home controller - Index Method. routes. MapRoute has attributes like name, url and defaults like controller name, action and id (optional).


1 Answers

Try this:

routes.MapRoute(
        "item_details",
        "item/{id}",
        new { controller = "item", action = "Get" }
        );
like image 129
robasta Avatar answered Oct 12 '22 04:10

robasta