Dr. Rahul Jha
Dr. Rahul Jha

Reputation: 1054

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.

Upvotes: 3

Views: 2247

Answers (1)

Ashley Lee
Ashley Lee

Reputation: 3976

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

Upvotes: 4

Related Questions