在大型项目开发的过程中,模块的数量可能会很多,仅仅只靠原有的目录结构是不能满足我们的要求的,而且扩展性也不是太好,因为思想就被束缚在它本身的结构上,扩展谈何容易。但真正的作为一个企业级架构这肯定是不行的,最起码走起来比较困难。于是控制器组,个性化的目录就是解决方案的开始。
对于自定义视图的关键还是如下:WebFormViewEngine 定义了视图文件的显示,我们可以继承改类从而重载覆盖父类的方法达到自己定义文件映射路径的目的。
public class AreaViewEngine:WebFormViewEngine
{
public AreaViewEngine()
: base()
{
//定义页面文件或者用户控件文件的路径规则
ViewLocationFormats = new string[] {
"~/{0}.aspx",
"~/{0}.ascx",
"~/Views/{1}/{0}.aspx",
"~/Views/{1}/{0}.ascx",
"~/Views/Shared/{0}.aspx",
"~/Views/Shared/{0}.ascx"
};
//第一母版页
文件路径规则
MasterLocationFormats = new string[] {
"~/{0}.master",
"~/Shared/{0}.master",
"~/Views/Shared/{0}.master",
"~/Views{1}/{0}.master"
};
PartialViewLocationFormats = ViewLocationFormats;
}
/// <summary>
/// 匹配页面文件的,寻找相应的部分视图文件
/// </summary>
/// <param name="controllerContext"></param>
/// <param name="partialViewName"></param>
/// <param name="useCache"></param>
/// <returns></returns>
public override ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache)
{
ViewEngineResult viewResult = null;
if (controllerContext.RequestContext.RouteData.Values.ContainsKey("area"))
{
string areaPartialName = FormatViewName(controllerContext,partialViewName);
viewResult = base.FindPartialView(controllerContext,areaPartialName,useCache);
if (viewResult != null && viewResult.View != null)
{
return viewResult;
}
string sharedAreaPartialName = FormatPartialViewName(controllerContext, partialViewName);
viewResult = base.FindPartialView(controllerContext,sharedAreaPartialName,useCache);
if (viewResult != null && viewResult.View != null)
{
return viewResult;
}
}
return base.FindPartialView(controllerContext, partialViewName, useCache);
}
/// <summary>
/// 匹配页面文件的,寻找相应的试图文件
/// </summary>
/// <param name="controllerContext"></param>
/// <param name="viewName"></param>
/// <param name="masterName"></param>
/// <param name="useCache"></param>
/// <returns></returns>
public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
{
ViewEngineResult viewResult = null;
if (controllerContext.RequestContext.RouteData.Values.ContainsKey("area"))
{
string areaPartialName = FormatViewName(controllerContext, viewName);
viewResult = base.FindView(controllerContext, areaPartialName,masterName, useCache);
if (viewResult != null && viewResult.View != null)
{
return viewResult;
}
string sharedAreaPartialName = FormatPartialViewName(controllerContext, viewName);
viewResult = base.FindView(controllerContext, sharedAreaPartialName,masterName, useCache);
if (viewResult != null && viewResult.View != null)
{
return viewResult;
}
}
return base.FindView(controllerContext, viewName, masterName, useCache);
}
/// <summary>
/// 这个匹配一般的页面文件或用户控件路径
/// </summary>
/// <param name="context"></param>
/// <param name="viewName"></param>
/// <returns></returns>
public string FormatViewName(ControllerContext context, string viewName)
{
//根据路由的参数值获得控制器的名称
string controllerName = context.RouteData.Get
requiredString("controller");
//根据路由的参数获得控制器组的名称
string area = context.RequestContext.RouteData.Values["area"].ToString();
return "Area/" + area + "/Views/" + controllerName + "/"+viewName ;
}
/// <summary>
/// 这个一般匹配母版页的文件路径
/// </summary>
/// <param name="context"></param>
/// <param name="viewName"></param>
/// <returns></returns>
public string FormatPartialViewName(ControllerContext context, string viewName)
{
//根据路由的参数获得控制器组的名称
string area = context.RequestContext.RouteData.Values["area"].ToString();
return "Area/" + area + "/Views/Shared/" + viewName;
}
}