我只是 MVC 的新手。
我已经开始阅读 Professional ASP.NET MVC3
作者:乔恩·加洛韦、菲尔·哈克、布拉德·威尔逊、斯科特·艾伦
在尝试学习如何创建自定义 View 时,我看到了一个名为“ReleaseView”的方法。我用谷歌搜索了一下并找到了它的定义。
我的问题是:何时调用方法(ReleaseView)?还有哪些地方可以使用它?
msdn上ReleaseView的定义是
Releases the specified view by using the specified controller context
。那么,我可以在我的 Controller 操作中使用这个方法吗?
如果我错了,请建议我
最佳答案
When it the method(ReleaseView) gets called?
它由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);
}
}
请注意,一旦 View 呈现到输出流,ReleaseView 方法就会被调用。因此,基本上每次 Controller 操作返回 View 或 PartialView 时,当此 ActionResult 完成执行时,它将调用底层 View 引擎上的 ReleaseView 方法。
And where are the other places it can be used ?
例如,如果您正在编写自定义 ActionResults。
So, can i make use of this method in my controller action ?
不, Controller 操作在 View 引擎开始执行之前就已经完成执行。
关于asp.net-mvc-3 - ReleaseView什么时候被调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14674902/