首页 > 代码库 > ASP.NET MVC 项目分离

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

ASP.NET MVC 项目分离