码迷,mamicode.com
首页 > Web开发 > 详细

ASP.NET MVC 项目分离

时间:2016-12-01 14:28:40      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:dal   cnn   get   jce   rnn   继承   ucf   nps   asp   

说明:

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 项目分离

标签:dal   cnn   get   jce   rnn   继承   ucf   nps   asp   

原文地址:http://www.cnblogs.com/fuqiang88/p/6121530.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!