ASP.NET Save HTML sent to browser
I need to keep the complete and accurate HTML sent to the browser for some transactions (for legal tracking purposes.) I suspect I'm not sure if there is a suitable hook for this. Somebody knows? (By the way, I am aware of the need to also save related pages such as style sheets and images.)
You can create a http module and keep the output stream.
You have to connect to the PreSendRequestContent ... event:
This event fires just before ASP.NET sends response content to the client. This event allows us to modify the content before it is delivered to the client. We can use this event to add content that is common to all pages to render the page. For example, general menu, header or footer.
a source to share
You can attach to PreSendRequestContent . This event is raised just before the content is posted and gives you the option to change it or, in your case, save it.
a source to share
You can implement a response filter. Here is a nice sample that parses the HTML generated by ASP.NET. In addition to sending HTML to the client, you can also write HTML to a database or other suitable storage.
Here's an alternative and IMO much easier to connect a filter to your application:
in Global.asax, put the following code in your Application_BeginRequest handler:
void Application_BeginRequest(object sender, EventArgs e)
{
Response.Filter = new HtmlSavingFilter(Response.Filter);
}
a source to share
I am assuming that you only want to keep the html rendering for specific pages. If so, I used the following approach in one of my applications, which stores the rendered html for caching purposes somewhere on disk. This method simply overrides the page render event.
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);
}
}
Works really well for me.
a source to share