【问题标题】:Rename the Controller Name in the URL重命名 URL 中的控制器名称
【发布时间】:2015-03-26 23:41:13
【问题描述】:

我有 2 个品牌使用相同的控制器。在一个品牌中,URL 必须看起来像 ~Home/Index,而在另一个 URL 中应该看起来像 ~Account/Index,但两个 URL 必须指向相同的 Home Controller 操作方法。

请提供任何想法如何实现这一点。

【问题讨论】:

  • AccountIndex()方法中,重定向到/Home/Index
  • 我只有一个控制器“Home”。但是 URL 应该显示为 Account for the brand2。
  • 然后创建一个路由 - url: "Account/Index", defaults: new { controller = "Home", action = "Index" }

标签: asp.net asp.net-mvc-4 asp.net-mvc-routing


【解决方案1】:

RouteConfig.cs中指定2个路由

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

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

使用此 URL 调用 Home 控制器中的 Index() 方法

1) http://localhost/../Home/Index

2) http://localhost/../Account/Index

【讨论】:

    【解决方案2】:

    将以下代码放入 Global.asax.cs

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

    *注意:在 gobal.asax.cs 的最后一行中始终放置默认名称

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-14
      • 1970-01-01
      • 1970-01-01
      • 2021-02-16
      • 2014-08-16
      • 2011-03-11
      • 2013-08-15
      相关资源
      最近更新 更多