Microsoft KB Archive/810205

= HOW TO: Archive the Results of a Dynamic Page to an HTML Page in ASP.NET by Using Visual C# .NET =

Article ID: 810205

Article Last Modified on 5/21/2003

-

APPLIES TO


 * Microsoft ASP.NET 1.1
 * Microsoft ASP.NET 1.0
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-





For a Microsoft Visual Basic .NET version of this article, see 811162.



IN THIS TASK


 * SUMMARY
 * Create a Web Form
 * Create the Response Filter Class
 * Use the Response Filter Class
 * Test the Response Filter
 * REFERENCES



SUMMARY
This step-by-step article describes how to retrieve the HTML results of a page as a stream and then download the stream to a file. When you use the FileStream object and set the Response.Filter property to the FileStream object, all HTTP output that Response.Write sends also downloads as stream to a file.

back to the top

Create a Web Form
To create a Web Form:  In Visual C# .NET, create a new ASP.NET Web Application project named ASPNETFilter. Right-click the designer pane of WebForm1.aspx. Click View HTML Source to edit the HTML code.  Replace the existing code with the following code : <%@ Page language=&quot;c#&quot; Codebehind=&quot;WebForm1.aspx.cs&quot; AutoEventWireup=&quot;false&quot; Inherits=&quot;ASPNETFilter.WebForm1&quot; %>   SaveResponse   <asp:TextBox ID=&quot;TextBox1&quot; Text=&quot;Textbox 1&quot; Runat=&quot;server&quot; /> <asp:ListBox ID=&quot;Listbox1&quot; Runat=&quot;server&quot; Size=&quot;3&quot;> <asp:ListItem Value=&quot;0&quot;>Zero</asp:ListItem> <asp:ListItem Value=&quot;1&quot; Selected=&quot;True&quot;>One</asp:ListItem> <asp:ListItem Value=&quot;2&quot;>Two</asp:ListItem> </asp:ListBox> <asp:CheckBox ID=&quot;Checkbox1&quot; Runat=&quot;server&quot; Checked=&quot;True&quot; Text=&quot;Checkbox 1&quot; /> </HTML> </li></ol>

back to the top

Create the ResponseFilter Class
To create the ResponseFilter class: <ol> Add a new class named ResponseFilter.cs.</li>  Replace the existing code with the following code: using System; using System.IO;

namespace ASPNETFilter {   public class ResponseFilter : Stream {       private Stream m_sink; private long m_position; private FileStream fs;

public ResponseFilter(Stream sink) {           m_sink = sink; fs = new FileStream(@&quot;C:\FilterOutput\response.htm&quot;, FileMode.OpenOrCreate, FileAccess.Write); }

// The following members of Stream must be overriden. public override bool CanRead {get { return true; }}

public override bool CanSeek {get { return false; }}

public override bool CanWrite {get { return false; }}

public override long Length {get { return 0; }}

public override long Position {           get { return m_position; } set { m_position = value; } }

public override long Seek(long offset, System.IO.SeekOrigin direction) {           return 0; }

public override void SetLength(long length) {           m_sink.SetLength(length); }

public override void Close {           m_sink.Close; fs.Close; }

public override void Flush {           m_sink.Flush; }

public override int Read(byte[] buffer, int offset, int count) {           return m_sink.Read(buffer, offset, count); }

// Override the Write method to filter Response to a file. public override void Write(byte[] buffer, int offset, int count) {                  //Write out the response to the browser. m_sink.Write(buffer, 0, count); //Write out the response to the file. fs.Write(buffer, 0, count); }   } }  Note Before you run the Web application: <ol> Create a folder named C:\FilterOutput.</li> Grant read and write access on the folder for the ASPNET user.</li></ol> </li></ol>

back to the top

Use the Response Filter Class
<ol> In Solution Explorer, select WebForm1.aspx.</li> Right-click and then select View Code.</li>  Add the following code to theOnInit event code: Response.Filter = new ResponseFilter(Response.Filter); </li></ol>

back to the top

Test the ResponseFilter
<ol> Save the changes to the ASPNETFilter Web project.</li> On the 'Build' menu, select 'Build Solution'.</li>  Start Microsoft Internet Explorer, and then open WebForm1.aspx by specifying the following URL, where  is the name of your Microsoft Internet Information Services (IIS) server: http://IISServerName/ASPNETFilter/WebForm1.aspx </li> Find the Response.htm file in the C:\FilterOutput folder.</li></ol>

back to the top

<div class="references_section">