asp.net-mvc-3 - ReleaseView什么时候被调用?

标签 asp.net-mvc-3 viewengine

我只是 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/

相关文章:

javascript - 如何在 Orchard CMS 中使用 javascript(js 文件)

asp.net - webGrid.Column 不允许更改列宽

c# - Html.BeginForm Post 转到 HttpGet 操作而不是 IE 中的 HttpPost,在 Chrome 和 Firefox 中很好

asp.net-mvc - 使用替代 View 引擎有哪些好处?

asp.net - 替代 asp.net MVC View 引擎

Razor 发电机 : how to use view compiled in a library as the partial view for master defined in main mvc project

javascript - 使用 Highcharts 并在没有数据时在图表上方或上方显示消息

asp.net-mvc-3 - 在 IIS7.5 网站中托管多个 ASP.Net MVC3 应用程序时出现问题

asp.net-mvc-3 - Razor View 引擎不搜索区域 View 位置

c# - 如何更改 asp.net mvc 中搜索 viewLocation 的顺序?