ASP.NET MVC创建视图过程

MvcHandler.ProcessRequest()

 

(1)获取Action执行结果:context2.Result

ActionExecutedContext context2 = this.InvokeActionMethodWithFilters(controllerContext, filters.ActionFilters, actionDescriptor, parameterValues
 
this.InvokeActionResultWithFilters(controllerContext, filters.ResultFilters, context2.Result);
 
(2)InvokeActionResultWithFilters()方法中:
this.InvokeActionResult(controllerContext, actionResult);
actionResult.ExecuteResult(controllerContext);
 如果是创建视图,实则执行:ViewResultBase.ExecuteResult();

(3)ViewResultBase.ExecuteResult()方法中:


ViewResultBase.
ExecuteResult()方法中:(找到视图引擎,找到视图)
result = this.FindView(context);
this.View = result.View;
实则执行:ViewResult.FindView()
(4)ViewResult.FindView()方法中:
ViewResult.FindView()方法中:
ViewEngineResultresult = base.ViewEngineCollection.FindView(context, base.ViewName, this.MasterName);
 
ViewResultBase属性 ViewEngineCollection

publicViewEngineCollectionViewEngineCollection
{
get
{
return (this._viewEngineCollection ?? ViewEngines.Engines);
// ViewEngines.Engines为静态ViewEngineCollection类型
       //静态构造函数中 为ViewEngineCollection中添加了两个视图引擎对象(WebFormViewEngine,RazorViewEngine)
}
set
{
this._viewEngineCollection = value;
}
}

(5)
ViewEngineCollection.FindView()方法:

publicvirtualViewEngineResultFindView(ControllerContextcontrollerContext, stringviewName, stringmasterName)
{
if (controllerContext == null)
{
thrownewArgumentNullException("controllerContext");
}
if (string.IsNullOrEmpty(viewName))
{
thrownewArgumentException(MvcResources.Common_NullOrEmpty, "viewName");
}
returnthis.Find(e => e.FindView(controllerContext, viewName, masterName, true), (Func<IViewEngine, ViewEngineResult>) (e => e.FindView(controllerContext, viewName, masterName, false)));
//e.FindView()为 IViewEngine.FindView()
}
 

(6)IViewEngine.FindView()方法:
如果为WebFormViewEngine、RazorViewEngine类,调用其父类的FindView()方法
 
FindView() 返回 ViewEngineResult类型


ViewEngineResult:(包含IView、IViewEngine类型的两个属性)
publicclassViewEngineResult
{
// MethodspublicViewEngineResult(IEnumerable<string> searchedLocations);
publicViewEngineResult(IViewview, IViewEngineviewEngine); // PropertiespublicIEnumerable<string> SearchedLocations { get; privateset; }
publicIViewView { get; privateset; }
publicIViewEngineViewEngine { get; privateset; }
}
 

IViewEngine:

publicinterfaceIViewEngine
{
// MethodsViewEngineResultFindPartialView(ControllerContextcontrollerContext, stringpartialViewName, booluseCache);
ViewEngineResultFindView(ControllerContextcontrollerContext, stringviewName, stringmasterName, booluseCache);
voidReleaseView(ControllerContextcontrollerContext, IViewview);
}


IView:
publicinterfaceIView
{
// MethodsvoidRender(ViewContextviewContext, TextWriterwriter);
}
自定义视图实现过程:
1.创建视图引擎,实现IViewEngine接口,实现FindView()方法,返回ViewEngineResult对象
2.创建视图
 
例:FindView()方法中:
return new ViewEngineResult(new WebFormView(controllerContext, viewPath, null), this);
return new ViewEngineResult(new RazorView(controllerContext, viewPath, null), this);
 
改变views目录后,需在相应的目录下添加web.config文件
注意:
需要将自定义视图引擎添加到 ViewEngines.Engines 视图引擎集合中
 
Application_start()中:
ViewEngines.Engines.Add(new DebugDataViewEngine()); 
 
在一个应用可以注册多个视图引擎,action调用器依次调用这些视图引擎的FindView方法,一旦某一个搜索引擎返回包含IView对象的ViewEngineResult结果调用停止,所以视图引擎注册的先后顺序是有影响的,可能存在两个视图引擎都可以处理同一个视图名称。如果我们想自定义的视图引擎优先处理可以将其插入列表首位:
 

ViewEngines.Engines.Insert(0, new DebugDataViewEngine());

 
 
 
 
 
上一篇:ASP.NET MVC创建静态页


下一篇:一些linux包相关命令