Question

I'm just a newbie to MVC.

I've started reading Professional ASP.NET MVC3 by Jon Galloway, Phil Haack, Brad Wilson, Scott Allen

I've seen a method named as `ReleaseView, when trying learning on how to create a custom view. I've googled about that and found a definition of it.

My Questions are: When it the method(ReleaseView) gets called? And where are the other places it can be used ?

The definetion of ReleaseView on msdn is that Releases the specified view by using the specified controller context. So, can i make use of this method in my controller action ?

Please suggest me if i'm going wrong

Was it helpful?

Solution

When it the method(ReleaseView) gets called?

It is called by the ViewResultBase.ExecuteResult method:

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);
    }
}

Notice how once the view is rendered to the output stream, the ReleaseView method is called. So basically every time a controller action returns a View or a PartialView, when this ActionResult completes executing it will call the ReleaseView method on the underlying view engine.

And where are the other places it can be used ?

For example if you are writing custom ActionResults.

So, can i make use of this method in my controller action ?

No, the controller action has finished executing much before the view engine starts executing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top