MVC自定义路由实现城市目录 如: http://www.xx.com/beijing

其它还是走MVC默认路由

只需修改路由就可以:

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // 路由名称
                "{controller}/{action}/{id}", // 带有参数的 URL
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // 参数默认值
                new { controller = @"News|Food|Game|Home|Account" }
            );

            routes.MapRoute(
                 "RootCity", // 路由名称
                 "{id}", // 带有参数的 URL
                 new
                 {
                     controller = "RootCity",
                     action = "Index",
                     id = UrlParameter.Optional
                 },
                 new { id = "\\w+" });

        }

需要注意的是要在默认规则上面加个正规则,只有在这里面的才走默认,其它都走我们自定义规则。

相关文章:

  • 2021-10-20
  • 2021-07-20
  • 2022-01-07
  • 2021-06-24
  • 2021-11-22
  • 2022-12-23
  • 2021-06-20
猜你喜欢
  • 2021-08-12
  • 2022-12-23
  • 2021-06-28
  • 2018-05-11
  • 2022-12-23
  • 2021-06-15
相关资源
相似解决方案