如何在asp.net mvc中更改url中的控制器名称

时间:2015-03-20 08:53:35

标签: asp.net-mvc url routing

我有一个" HomeController"我的路线是这样的

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

我希望网址像#34;网页设计/索引"而不是" Home / Index"。
我怎么能这样做? 感谢

2 个答案:

答案 0 :(得分:3)

我会这样做:

  // add a new route
  routes.MapRoute(
        name: "homepage",
        url: "web-design/{action}",
        defaults: new { 
        controller = "Home", action ="Index"
        }   
    );

  // add your default route but change the default action or controller
  routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { 
        controller = "Home", action ="SomeotherAction", id =    UrlParameter.Optional
        }   
    );

答案 1 :(得分:1)

如果你想要" web-design / Index"成为默认路线:

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