MVC3 自定义路由实现城市目录

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+" });

        }

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

原文地址:https://www.cnblogs.com/hantianwei/p/2579133.html