Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to map a controller name with another name in mvc

Can I put another name for my controller. I mean let say I have a controller "Home". Now can I create a route or something like this so that I can call my url /home/index as /home/index or as /start/index. I want to use "start" instead of "home" but also want to use "Home" in some place.

like image 821
Rahul Jha Avatar asked Dec 11 '25 18:12

Rahul Jha


1 Answers

You can add this to your RouteConfig.cs file above your default routing like this:

routes.MapRoute(
    name: "AlternateHome",
    url: "Start/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

You could also use attribute routing to only affect your Index action in the Home Controller and add 2 attribute routes. http://blogs.msdn.com/b/webdev/archive/2013/10/17/attribute-routing-in-asp-net-mvc-5.aspx

like image 110
Ashley Lee Avatar answered Dec 13 '25 09:12

Ashley Lee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!