views:

697

answers:

5

I need to save the full and exact HTML sent to the browser for some transactions (for legal tracking purposes.) I suspect I am not sure if there is a suitable hook to do this. Does anyone know? (BTW, I am aware of the need to also save associated pages like style sheets and images.)

+1  A: 

You could implement a response filter. Here is a nice sample that processes the HTML produced by ASP.NET. In addition to the HTML being sent to the client you should be able to also write the HTML to a database or other suitable storage.

Here is an alternate and IMO much easier way to hook the filter into your application:

in Global.asax, place the following code in the Application_BeginRequest handler:

void Application_BeginRequest(object sender, EventArgs e)
{
    Response.Filter = new HtmlSavingFilter(Response.Filter);
}
Peter Lillevold
+2  A: 

You can create an http module and have the output stream saved somewhere.

You should hook to PreSendRequestContent event...:

This event is raised just before ASP.NET sends the response contents to the client. This event allows us to change the contents before it gets delivered to the client. We can use this event to add the contents, which are common in all pages, to the page output. For example, a common menu, header or footer.

Rashack
+2  A: 

You could attach to the PreSendRequestContent. This event is raised right before the content is sent and gives you a chance to modify it, or in your case, save it.

P&P article on interception pattern

Darren Kopp
A: 

There are also hardware devices made specifically for this purpose. We've used one called "PageVault".

David
A: 

I suppose you only want to save the rendered html for certain pages. If so, I have been using the following approach in one of my applications that stores the rendered html for caching purpose somewhere on the disk. This method simply overrides the render event of the page.

protected override void Render(HtmlTextWriter writer)
{
    using (HtmlTextWriter htmlwriter = new HtmlTextWriter(new System.IO.StringWriter()))
    {
        base.Render(htmlwriter);
        string html = htmlwriter.InnerWriter.ToString();


        using (FileStream outputStream = new FileStream(@"C:\\temp.html", FileMode.OpenOrCreate))
        {
             outputStream.Write(html, 0, html.Length);
             outputStream.Close();
        }

        writer.Write(html);
    }
}

Really works well for me.

Joop