views:

203

answers:

1

I have tried to implement simple ViewPage caching with ASP.NET MVC, however I cannot get the page to render correctly into a custom HtmlTextWriter when it has a master page.

I have tried overriding Render. If I simply call the base implementation, then everything renders correctly. If I render to my own writer and then write that string, then the page contents get scrambled.

Imports System.IO

Public Class CachedViewPage
Inherits System.Web.Mvc.ViewPage

Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
    'MyBase.Render(writer)
    'Return

    Dim stringView As String
    Using sw As New StringWriter
        Using w As New HtmlTextWriter(sw)
            MyBase.Render(w)
        End Using
        stringView = sw.ToString()
    End Using
    writer.Write(stringView)
End Sub

End Class

It would seem that there is a tie between the MasterPage, the ViewPage, and the HtmlTextWriter.

How should I properly render this ViewPage to a string?

+2  A: 

Are you aware of the built in caching that is provided by ASP.NET MVC? It may be more appropriate to use what is provided by the framework.

Here is a great overview; Donut Caching in ASP.NET MVC

As far as I know it is not possible to get the output of the view as a string (maybe if you override the Render in the master page?) without developing or using an alternative view engine.

There are some alternative view engines available in MVC Contrib if you are interested in taking a look.

spoon16