ASP.NET MVC 实现区域 项目分离 (比较好的方式)

说明:

ZRT.Web 是前台网站,目录【D:\ZRT.Web\】

ZRT.Admin 是后台管理,目录【D:\ZRT.Web\Applications\Admin\】,删除文件【Global.asax】

 

 

1. 继承 RazorViewEngine,重写路径模板

复制代码
namespace ZRT.Web.Framework
{
    public class CustomerRazorViewEngine : RazorViewEngine
    {
        public CustomerRazorViewEngine()
        {
            AreaViewLocationFormats = new[]
                                          {
                                              //default
                                              "~/Applications/{2}/Views/{1}/{0}.cshtml", 
                                              "~/Applications/{2}/Views/Shared/{0}.cshtml"
                                          };

            AreaMasterLocationFormats = new[]
                                            {
                                                //default
                                                "~/Applications/{2}/Views/{1}/{0}.cshtml", 
                                                "~/Applications/{2}/Views/Shared/{0}.cshtml"
                                            };

            ViewLocationFormats = new[]
                                      {
                                            //default
                                            "~/Views/{1}/{0}.cshtml", 
                                            "~/Views/Shared/{0}.cshtml"
                                      };

            MasterLocationFormats = new[]
                                        {
                                            //default
                                            "~/Views/{1}/{0}.cshtml", 
                                            "~/Views/Shared/{0}.cshtml"
                                        };

            AreaPartialViewLocationFormats = AreaViewLocationFormats;
            PartialViewLocationFormats = ViewLocationFormats;
        }
    }
}
复制代码

2. 在ZRT.Web的 Global.asax 文件里 设置试图引擎

复制代码
public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(new CustomerRazorViewEngine());
        }
    }
复制代码

3. 修改默认网站的路由,添加命名空间

复制代码
public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                namespaces: new[] { "ZRT.Web.Controllers" }
            );
        }
    }
复制代码

4. 注册区域,在ZRT.Admin项目里添加一个类,AdminAreaRegistration 继承 AreaRegistration

复制代码
namespace ZRT.Admin
{
    public class AdminAreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get
            {
                return "Admin";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "Admin_default",
                "Admin/{controller}/{action}/{id}",
                new { controller = "Home", action = "Index", area = "Admin", id = "" },
                new[] { "ZRT.Admin.Controllers" }
            );
        }
    }
}
复制代码

5. 修改 ZRT.Admin项目 Views目录下 _ViewStart.cshtml 文件里的布局路径

@{
    Layout = "~/Applications/Admin/Views/Shared/_Layout.cshtml";
}

6. 修改 ZRT.Admin项目的输出路径

 

 

附DEMO源码

http://www.cnblogs.com/sealzrt/p/3628357.html

posted on 2016-12-01 13:28  扶强  阅读(1479)  评论(0编辑  收藏  举报