c# mvc 同一个站点里同时部署web与wap,web下属wap站点路由配置

要达到的目录结构:

1。web站,电脑站

主页

/

/Home

频道页

/Channel1/List

/Channel1/Detail

/Channel2/List

用户

/User/username1/Index

/User/username1/Contact

2。wap站,手机站

主页

/Wap/

/Wap/Home

频道页

/Wap/Channel1/List

/Wap/Channel1/Detail

/Wap/Channel2/List

用户

/Wap/User/username1/Index

/Wap/User/username1/Contact

 

 

测试环境,

vs2015,net4.5,mvc,5.2.3,

配置1。

像电脑站Channel1,这种目录结构,相对比较简单,

routes.MapRoute(
name: "Channel1",//这个是路由名称,一般是所有路由名称不一样,有一样的可能会有什么不妥吧,
url: "Channel1/{username}/{action}/{id}",//这里指定了Channel1/  的路径下作为筛选路由
defaults: new { controller = "Channel1", action = "Index", username = UrlParameter.Optional,id=UrlParameter.Optional },//这里指定默认控制器,action方法,及可以接收两个变量
namespaces: new string[] { "Web.Controllers" }//这里是控制器的名空间,如果没有重名可以不要,这里有wap重名,还是需要的,
);

 

需要注意的是,路由添加的先后顺序,也是要注意的,它会先匹配先添加的路由规则,

以下是全部的 RouteConfig规则,(这个规则看起来好像,wap站少挺多的,其实,如果没有wap站,只是web站的话,只需要配置两个规则,一个User,一个Default,因为控制器可以动态,但是有wap站,有些规则会被覆盖,原因有待慢慢找)

    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.MapRoute(
	name: "User",
	url: "User/{username}/{action}/{id}",
	defaults: new { controller = "User", action = "Index", username = UrlParameter.Optional,id=UrlParameter.Optional },
	namespaces: new string[] { "Web.Controllers" }
    );
    routes.MapRoute(
	name: "Home",
	url: "Home/{action}/{id}",
	defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
	namespaces: new string[] { "Web.Controllers" }
    );
    routes.MapRoute(
	name: "Channel1",
	url: "Channel1/{action}/{id}",
	defaults: new { controller = "Channel1", action = "Index", id = UrlParameter.Optional },
	namespaces: new string[] { "Web.Controllers" }
    );
    routes.MapRoute(
	name: "Wap-User",
	url: "Wap/User/{username}/{action}/{id}",
	defaults: new { controller = "User", action = "Index", username = UrlParameter.Optional, id = UrlParameter.Optional },
	namespaces: new string[] { "Web.Controllers.Wap" }
    );
    routes.MapRoute(
	name: "Wap",
	url: "Wap/{controller}/{action}/{id}",
	defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
	namespaces: new string[] { "Web.Controllers.Wap" }
    );
    routes.MapRoute(
	name: "Default",
	url: "{controller}/{action}/{id}",
	defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
	namespaces: new string[] { "Web.Controllers" }
    );

  配置2,继承RazorViewEngine,重写一些方法,

需要注意,location_formates的顺序,以及FindView方法的重写,FindView做重找视图,可以找到自己想要的视图,没有做这一步,有可能web站的url得到的确是wap站的cshtml

    public class MyViewEngine: RazorViewEngine
    {
        public MyViewEngine()
        {
            var location_formates = new[]
            {
                "~/Views/Wap/User/{1}/{0}.cshtml",
                "~/Views/Wap/{1}/{0}.cshtml",
                "~/Views/User/{1}/{0}.cshtml",
                "~/Views/{1}/{0}.cshtml",
            };
            ViewLocationFormats = location_formates;
            PartialViewLocationFormats = location_formates;
        }
        public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
        {
            string sp1 = ((System.Web.Routing.Route)controllerContext.RouteData.Route).Url.ToLower();
            string sp2 = "";
            if (string.IsNullOrEmpty(sp1))                sp2 = "";
            else                sp2 = sp1.Split('/')[0];
            switch (sp2)
            {
                case "home":
                    return new ViewEngineResult(CreateView(controllerContext, "~/Views/Home/" + viewName + ".cshtml", ""), this);
                case "channel1":
                    return new ViewEngineResult(CreateView(controllerContext, "~/Views/Channel1/" + viewName + ".cshtml", ""), this);
                case "user":
                    return new ViewEngineResult(CreateView(controllerContext, "~/Views/User/" + viewName + ".cshtml", ""), this);
            }
            if (controllerContext.Controller.GetType() == typeof(Web.Controllers.HomeController)) {
                if (controllerContext.HttpContext.Request.Url.AbsolutePath == "/") {
                    return new ViewEngineResult(CreateView(controllerContext, "~/Views/Home/Index.cshtml", ""), this);
                }
            }
            
            return base.FindView(controllerContext, viewName, masterName, useCache);
        }
    }

  配置3。控制器结构

Web.Controllers

    HomeController

    Channel1Controller

    UserController

Web.Controllers/Wap

    HomeController

    Channel1Controller

    UserController

配置4。View的路径结构

Views/

Home/Index.cshtml

Channel1/Index.cshtml

User/Index.cshtml

Views/Wap/

Home/Index.cshtml

Channel1/Index.cshtml

User/Index.cshtml

 

posted @ 2017-07-29 09:39  以函  阅读(246)  评论(0编辑  收藏  举报