Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use MVC routing to alias a controller

I have a controller called InstallationController, and a fancy report representation of an installation called a Rate Card, but the end user insists on calling installations themselves Rate Cards. I would like him to see the URL http://site/RateCard/Edit/3, where this gets routed actually as http://site/Installation/Edit/3. How can I do this in MVC 3 RC2?

like image 879
ProfK Avatar asked Dec 16 '10 20:12

ProfK


People also ask

How do you provide an alias name for an action method in Web API?

An alias name for an action method is provided by using ActionName attribute. Also it is necessary to change the route template in the WebApiConfig. cs.

What is ActionName in C#?

CSharp Online Training ActionName attribute is an action selector which is used for a different name of the action method. We use ActionName attribute when we want that action method to be called with a different name instead of the actual name of the method.

How does MVC routing work?

ASP.NET MVC Routing does the same thing; it shows the way to a request. Basically, routing is used for handling HTTP requests and searching matching action methods, and then executing the same. It constructs outgoing URLs that correspond to controller actions. Routing the map request with Controller's Action Method.

How controller is called in MVC?

A controller is just a class (for example, a Visual Basic or C# class). The sample ASP.NET MVC application includes a controller named HomeController. cs located in the Controllers folder.


1 Answers

A couple of options are, you can either rename the controller to RateCardController, or add a new route that directs to the Installation controller, like:

routes.MapRoute(
               "RateCard", // Route name
               "RateCard/{action}/{id}", // URL with parameters
               new { controller = "Installation", action = "Index", id = UrlParameter.Optional } // Parameter defaults
               );
like image 182
Eric King Avatar answered Nov 02 '22 23:11

Eric King