Take this scenario: on a website a visitor fills in a form after which the form is to be filed as a static HTML page. you might have several reasons for wanting to do that, one of them are search engines. getting it done in Asp.net took me more effort than I had originally expected. let me share what I have found to be working.
The easy way to catch the HTML rendered by Asp.net is to save a page in the client browser. to catch it on the server you have to hook into the (HTML) rendering process. there is noPage. onrenderEvent butPageClass does haveRenderMethod. The method has a protected visibility, you cannot invoke it form your code.RenderMethod is invoked when Asp.net renders the page to the output, to hook in you override the method. The render method has a parameter of TypeHtnltextwriter, The default implementation of the overriden render method is invokingBase. RenderPassing itHtmltextwriterObject.
Protected override void render (htmltextwriter writer)
{
// Default behavior
Base. Render (writer );
}
This gives a place to hook in an own textwriter where ASP, net can render the HTML. the constructor of the htmltextwriter class has a protected visibility. passing an own htmltextwriter requires specializing the frameworks's htmltextwriter class. the constructor of the base class requires a stringwriter. A Wrapper class to house both htmlm htmltextwriter and stringwriter:
Internal class myhtmlfilecreator
{
Private stringwriter HTML;
Private myhtmltextwriter htmlwriter;
// Override the htmltextwriter to reach the constructor
// The constructor in the base class is protected
Class myhtmltextwriter: htmltextwriter
{
Internal myhtmltextwriter (textwriter tw): Base (TW ){}
}
// Publish the htmlwriter
Internal htmltextwriter renderhere
{
Get {return htmlwriter ;}
}
// Constructor initializes stringwriter and htmlwriter based on that
// Initialize URL
Internal myhtmlfilecreator ()
{
Html = new stringwriter ();
Htmlwriter = new myhtmltextwriter (HTML );
Newurl = context. Request. url. absolutepath. tostring ();
Newurl = newurl. Replace (". aspx", ". htm ");
}
Internal void writehtmlfile (string virtualfilename)
{
// Stringreader reads output rendered by Asp.net
// Stringwriter writes HTML output file
Stringreader sr = new stringreader (html. tostring ());
Stringwriter Sw = new stringwriter ();
// Read from input
String htmlline = Sr. Readline ();
While (htmlline! = NULL)
{
// Filter out Asp.net specific tags
If (! (Htmlline. indexof ("<form")> 0) |
(Htmlline. indexof ("_ viewstate")> 0) |
(Htmlline. indexof ("</form>")> 0 )))
{Sw. writeline (htmlline );}
Htmlline = Sr. Readline ();
}
// Write contents stringwriter to HTML file
Streamwriter FS = new streamwriter (virtualfilename );
FS. Write (SW. tostring ());
FS. Close ();
}
}
A myhtmlfilecreator object has a place Asp.net can render to and it has a method to write the contents of the stringwriter (inside the htmlwriter) to file. I use the myhtmlfilecreator in a page base class. the page hooks in the render event. when the freeze flag is set the HTML is rendered to a file and the application is redirected to the HTML file just created.
Public class freezablepage: system. Web. UI. Page
{
Internal class myhtmlfilecreator {}
// When ASP. NET renders the page. Render method is invoked
// Override the method to hook in
Protected override void render (htmltextwriter writer)
{
If (freeze)
{
Myhtmlfilecreator htmlfile = new myhtmlfilecreator ();
// Let Asp.net render the output, catch it in the file creator
Base. Render (htmlfile. renderhere );
// Write new HTML file
Htmlfile. writehtmlfile (server. mappath (newurl ));
// Redirect
Response. Redirect (newurl, true );
}
Else
{
// Default behavior
Base. Render (writer );
}
}
// Flag render event
Protected void freeze ()
{
Freeze = true;
}
Protected void freeze (string tourl)
{
Freeze = true;
Newurl = tourl;
}
Private bool freeze = false;
Private string newurl;
Internal string newurl
{
Get
{
Return newurl;
}
Set
{
Newurl = value;
}
}
}
}
Use this on your page like this:
Public class _ default: Gekko. Web. UI. freezablepage
{
Private void button#click (Object sender, system. eventargs E)
{
Freeze (string. Format (@ "statisch/{0).htm", textbox1.text ));
}
}
You will see that the resulting page has lost its form knocking out Al buttons. there are no more PostBack possible. and the viewstate is also cut away. to process any file Asp.net might produce will require more filtering. you can do quite flexible things treating the input as XML. which requires some cutting and pasting, an HTML document is not always well-formed.
BTW Firefox was a lovely sidekick. The way it displays the HTML source of a page... Mmm:>
Peter