One is a page conversion class, which is implemented through the static function Changfile (), and the conversion from dynamic pages to static pages.
Copy codeThe Code is as follows:
Using System;
Using System. Data;
Using System. Configuration;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Using System. Text;
Using System. IO;
/**////
/// Summary description for HtmlProxy
///
Public class HtmlProxy
...{
Public HtmlProxy ()
...{
}
Public static bool ChangeFile (int id)
...{
String filename = HttpContext. Current. Server. MapPath ("Post _" + id + ". html ");
// Try to read the existing file Stream st = GetFileStream (filename );
// If the file exists and is read successfully
If (st! = Null)
...{
Using (st)
...{
StreamToStream (st, HttpContext. Current. Response. OutputStream );
Return true;
// Response. End ();
}
}
Else
...{
StringWriter sw = new StringWriter ();
HttpContext. Current. Server. Execute ("ForumDetail. aspx? PID = "+ id, sw );
String content = sw. ToString ();
// Write the file
Try
...{
Using (FileStream fs = new FileStream (filename, FileMode. Create, FileAccess. Write, FileShare. Write ))
...{
Using (StreamWriter stw = new StreamWriter (fs, HttpContext. Current. Response. ContentEncoding ))
...{
Stw. Write (content );
}
}
Return true;
}
Catch... {return false ;}
}
}
Private static Stream GetFileStream (string filename)
...{
Try
...{
DateTime dt = File. GetLastWriteTime (filename );
TimeSpan ts = dt-DateTime. Now;
If (ts. TotalHours> 1)
...{
// Expire in one hour
Return null;
}
Return new FileStream (filename, FileMode. Open, FileAccess. Read, FileShare. Read );
}
Catch... {return null ;}
}
Static public void StreamToStream (Stream src, Stream dst)
...{
Byte [] buf = new byte [1, 4096];
While (true)
...{
Int c = src. Read (buf, 0, buf. Length );
If (c = 0)
Return;
Dst. Write (buf, 0, c );
}
}
}
In the page file, the background code of ForURL. aspx is as follows:
Protected void Page_Load (object sender, EventArgs e)
...{
Try
...{
Int id = int. Parse (Request. QueryString ["PID"]);
If (HtmlProxy. ChangeFile (id ))
...{
Response. Redirect ("Post _" + id + ". html ");
}
Else
...{
Response. Redirect ("Post. aspx? PID = "+ id );
}
}
Catch ...{
}
}