首页 > 代码库 > 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;
}
}
}
{
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());
}
}
{
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" }
);
}
}
{
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" }
);
}
}
}
{
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";
}
Layout = "~/Applications/Admin/Views/Shared/_Layout.cshtml";
}
6. 修改 ZRT.Admin项目的输出路径
附DEMO源码
http://www.cnblogs.com/sealzrt/p/3628357.html
ASP.NET MVC 项目分离
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。