【问题标题】:when does ReleaseView get's called?ReleaseView 什么时候被调用?
【发布时间】:2013-02-03 17:03:55
【问题描述】:

我只是 MVC 的新手。

我已经开始阅读 Jon Galloway、Phil Haack、Brad Wilson、Scott Allen 的 Professional ASP.NET MVC3

我在尝试学习如何创建自定义视图时看到了一个名为“ReleaseView”的方法。我用谷歌搜索了一下,找到了它的定义。

我的问题是:何时调用方法(ReleaseView)?还有其他地方可以用吗?

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

如果我错了,请建议我

【问题讨论】:

    标签: asp.net-mvc-3 viewengine


    【解决方案1】:

    什么时候调用方法(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方法。

    还有其他地方可以用吗?

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

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

    不,控制器动作在视图引擎开始执行之前就已经完成执行。

    【讨论】:

      猜你喜欢
      • 2011-07-26
      • 2010-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多