Protected Void Button#click ( Object Sender, eventargs E)
{
String URL = " Http :// " + Request. url. Authority + " /Default. aspx " ;
New System. net. WebClient (). downloadfile (URL, server. mappath ( " ~ /Default.html " ));
Response. Redirect ( " Default.html " );
}
BeforeArticle: Asp.net static page generation demo
ThisCodeThe solution is also written based on the Code of many people. I would like to express my gratitude to these unsung contributors.
Paste a core code:
Using System;
Using System. Web;
Using System. Web. UI;
Using System. IO;
Using System. text;
Using System. Text. regularexpressions;
Using System. configuration;
Namespace Bll
{
/// <Summary>
/// Abstract description of pagebase.
/// </Summary>
Public Class Pagebase: Page
{
Private Static Object OBJ = New Object ();
/// / <Summary>
/// Override the default htmltextwriter method, modify the value attribute in the form tag, and set the value to the rewritten URL instead of the real URL.
/// </Summary>
/// <Param name = "Writer"> </param>
Protected Override Void Render (htmltextwriter writer)
{
String URL = Request. rawurl. tolower ();
If (Request. requesttype. tolower (). Equals ( " Get " ))
{
Urlrewrite u = (Urlrewrite) httpcontext. Current. items [ " Siteurl " ];
If (U ! = Null && U. Cache > 0 )
{
String Cachefile = Server. mappath ( " /Cache " + URL + " . Html " );
Fileutil. createdirectory (cachefile );
Lock (OBJ)
{
Using (Streamwriter SW = New Streamwriter (cachefile, False , Encoding. utf8 ))
{
Base . Render ( New Fetchhtmlwriter (writer, SW ));
Sw. Flush ();
Sw. Close ();
Sw. Dispose ();
}
Cache. Add (URL, String . Empty, Null , Datetime. Now. addseconds (U. cache), timespan. Zero, system. Web. caching. cacheitempriority. High, Null );
Return ;
}
}
}
If (Writer Is System. Web. UI. html32textwriter)
{
Writer = New Formfixerhtml32textwriter (writer. innerwriter );
}
Else
{
Writer = New Formfixerhtmltextwriter (writer. innerwriter );
}
Base . Render (writer );
}
}
Public Class Formfixerhtml32textwriter: system. Web. UI. html32textwriter
{
Public Formfixerhtml32textwriter (textwriter writer)
: Base (Writer)
{
}
Public Override Void Writeattribute ( String Name, String Value, Bool Encode)
{
// If the current output attribute is the form-marked action attribute, replace the value with the rewritten false URL.
If ( String . Compare (name, " Action " , True ) = 0 )
{
Value = Httpcontext. Current. Request. rawurl;
}
Base . Writeattribute (name, value, encode );
}
}
Public Class Formfixerhtmltextwriter: system. Web. UI. htmltextwriter
{
Public Formfixerhtmltextwriter (textwriter writer)
: Base (Writer)
{
}
Public Override Void Writeattribute ( String Name, String Value, Bool Encode)
{
If ( String . Compare (name, " Action " , True ) = 0 )
{
Value = Httpcontext. Current. Request. rawurl;
}
Base . Writeattribute (name, value, encode );
}
}
}
Download the code in detail: Asp.net generates a static page
If you have any questions, add the QQ group: 49745612 for further discussion.
Compared with the previous method, the method for generating static pages is much more complicated, and I prefer the previous implementation method.