【问题标题】:How to override default route path in web.config for asp.net mvc application如何在 web.config 中为 asp.net mvc 应用程序覆盖默认路由路径
【发布时间】:2015-03-15 20:33:56
【问题描述】:

是否可以通过修改 web.config 文件来覆盖 asp.net mvc 应用程序的默认路由?

不修改 routeconfig.cs 文件

【问题讨论】:

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


    【解决方案1】:

    我遇到了同样的问题,我通过以下解决方法解决了这个问题:

    假设,您的 web.config 中有一个格式为“控制器/操作”的路由元素。 在我的 RouteConfig.cs 中,我添加了以下代码:

    public static void RegisterRoutes(RouteCollection routes)
    {
      var mySection = 
        WebConfigurationManager.OpenWebConfiguration("~").GetSection("WHATEVER_YOU_NEED");
      if (mySection != null)
      {
        // OVERRIDE WITH WEB.CONFIG
        string[] parts = mySection["YOUR_GROUP"].Split('/');
        routes.MapRoute(
                    name: "Default",
                    url: "{controller}/{action}/{id}",
                    defaults: new 
                    { 
                       controller = parts[0], 
                       action = parts[1], 
                       id = UrlParameter.Optional 
                    });
      }
      else
      {
        routes.MapRoute( ... YOUR DEFAULT ROUTE ...);         
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-01
      • 2022-09-27
      • 1970-01-01
      • 2017-07-08
      • 1970-01-01
      相关资源
      最近更新 更多