2013-02-03 50 views
2

我只是MVC的新手。什么时候ReleaseView被调用?

我已经开始由乔恩·加洛韦,菲尔哈克,布拉德·威尔逊,斯科特 - 阿伦

我见过努力地学习如何创建自定义视图时命名为`ReleaseView,方法阅读Professional ASP.NET MVC3。我一直在搜索,并找到了它的定义。

我的问题是:当它的方法(ReleaseView)被调用?其他地方可以使用哪里?

msdn上ReleaseView的定义是 Releases the specified view by using the specified controller context。那么,我可以在我的控制器操作中使用这种方法吗?

请建议我,如果我去错了

回答

3

当的方法(ReleaseView)被调用?

它由ViewResultBase.ExecuteResult方法称为:

public override void ExecuteResult(ControllerContext context) 
{ 
    if (context == null) 
    { 
     throw new ArgumentNullException("context"); 
    } 
    if (string.IsNullOrEmpty(this.ViewName)) 
    { 
     this.ViewName = context.RouteData.GetRequiredString("action"); 
    } 
    ViewEngineResult result = null; 
    if (this.View == null) 
    { 
     result = this.FindView(context); 
     this.View = result.View; 
    } 
    TextWriter output = context.HttpContext.Response.Output; 
    ViewContext viewContext = new ViewContext(context, this.View, this.ViewData, this.TempData, output); 
    this.View.Render(viewContext, output); 
    if (result != null) 
    { 
     result.ViewEngine.ReleaseView(context, this.View); 
    } 
} 

通知如何一旦视图被呈现到输出流中,ReleaseView方法被调用。所以基本上每次控制器操作返回一个View或PartialView时,当这个ActionResult完成执行时,它将调用底层视图引擎的ReleaseView方法。

其他可以使用的地方在哪里?

例如,如果您正在编写自定义ActionResults。

那么,我可以在我的控制器操作中使用这种方法吗?

不,控制器操作在视图引擎开始执行之前已经完成了很多操作。

相关问题