1,我们的目标:先看目录结构图
默认在mvc 目录结构上,都是2层,当要做一个大的项目时,肯定不行。于是有了Area的功能,但是Area的结构里又包含了 一个完整 controlller ,views ,model ,很是烦人,
我们的正式项目,肯定是要将Controllers ,models,分到另外的工程里的,views 保留在 web项目里。在各自的项目里,按功能块用文件夹进行划分。
2,利用Area的原理。先新建一个 Area ,取名 Admin,观察它下面的 AdminAreaRegistration.cs 文件的内容,用来注册区域路由的。其他的不用管,看着都烦。
<p> public class AdminAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Admin";
}
}</p><p> public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
}</p>
这个类,是在 Global.asax 文件中被调用的,(原理之类的,这里一律不讲,我也讲不好。一切都是实现)
有一句: AreaRegistration.RegisterAllAreas();
既然这么注册区域了,我们自己写一些 ***AreaRegistration 放在Controller 目录里面。(或者把Area里的 AdminAreaRegistration.cs 拷贝走)具体位置和自己的文件夹一致就行了,AreaRegistration.RegisterAllAreas() 并没有要求位置,他只管找程序集里的 AreaRegistration。那么我们就不需要那个讨厌的Area 文件夹了,而是把Area 里的 Controllers ,models,views 分别拖到各自的工程里/或文件里。
唯一有用的AdminAreaRegistration.cs 文件被拿走之后,直接删了Area文件夹及里面的内容。
2,目录组织好,之后,再增加一个自定义的试图 规则:
public class MyCustomerRazorViewEngine : RazorViewEngine
{
public MyCustomerRazorViewEngine()
: base()
{
//Area视图路径其中{2},{1},{0}分别代表Area名,Controller名,Action名
AreaViewLocationFormats = new[] { "~/Views/{2}/{1}/{0}.cshtml", "~/Views/Shared/{0}.cshtml" };
//Area模版路径
AreaMasterLocationFormats = new[] { "~/Views/Shared/{0}.cshtml" };
//Area的分部视图路径
AreaPartialViewLocationFormats = new[] { "~/Views/{2}/{1}/{0}.cshtml", "~/Views/Shared/{0}.cshtml" };
//主视图路径
ViewLocationFormats = new[] { "~/Views/{1}/{0}.cshtml", "~/Views/Shared/{0}.cshtml" };
//主模版路径
MasterLocationFormats = new[] { "~/Views/Shared/{0}.cshtml" };
//主分部视图路径
PartialViewLocationFormats = new[] { "~/Views/{1}/{0}.cshtml", "~/Views/Shared/{0}.cshtml" };
}
protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
{
return base.CreatePartialView(controllerContext, partialPath);
}
protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
{
return base.CreateView(controllerContext, viewPath, masterPath);
}
protected override bool FileExists(ControllerContext controllerContext, string virtualPath)
{
return base.FileExists(controllerContext, virtualPath);
}
}
Gloabel 文件里,需要调用该规则:
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
<span style="color:#ff0000;">//注册自定义视图
</span>
<span style="color:#ff0000;"> ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new MyCustomerRazorViewEngine());
</span>
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
//BundleTable.EnableOptimizations = true;
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
}
}
3,默认的 _Layout.cshtml 文件里的连接,我改成了自己的:
<nav>
<ul id="menu">
<li><a href="/Admin/A1/AdminMange/index">AdminMange</a></li>
<li><a href="/Admin/A1/UserCus/index">UserCus</a></li>
<li><a href="/Log/LogCus/index">LogCus</a></li>
<li><a href="/Default/index">Default</a></li>
</ul>
</nav>
@Html.ActionLink 的方式,我玩的不好,生成的路径感觉不对,所以还是直接手写。其实都一样。@Html.ActionLink 其实也是写死的。本人没觉得哪好的。
4,代码使用方式,为了减少体积,没有 packages 文件夹,请用vs 2012 新建一个 mvc 项目后,将 MvcBundle.csproj 添加到解决方案里去 编译,运行。