There are many ways to convert Asp. Net Dynamic pages to static pages on the Internet. Based on actual needs, I found some source code on the Internet and made changes. Write the modified Code and description.
A class for Asp. Net dynamic page conversion. This class is implemented through the static function Changfile (), and Asp. Net dynamic page to Static Page conversion.
- 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)
- ...{
- StringFilename=HttpContext. Current. Server. MapPath ("Post _" + id + ". html ");
- // Try to read the existing file StreamSt=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
- ...{
- StringWriterSw=NewStringWriter ();
- HttpContext. Current. Server. Execute ("ForumDetail. aspx?PID= "+ Id, sw );
- StringContent=Sw. ToString ();
- // Write the file
-
- Try
- ...{
- Using (FileStreamFs=NewFileStream (filename, FileMode. Create, FileAccess. Write, FileShare. Write ))
- ...{
- Using (StreamWriterStw=NewStreamWriter (fs, HttpContext. Current. Response. ContentEncoding ))
- ...{
- Stw. Write (content );
- }
- }
- Return true;
- }
- Catch... {return false ;}
- }
- }
- Private static Stream GetFileStream (string filename)
- ...{
- Try
- ...{
- DateTimeDt=File. GetLastWriteTime (filename );
- TimeSpanTs=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=NewByte [2, 4096];
- While (true)
- ...{
- IntC=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
- ...{
- IntId=Int. Parse (Request. QueryString ["PID"]);
- If (HtmlProxy. ChangeFile (id ))
- ...{
- Response. Redirect ("Post _" + id + ". html ");
- }
- Else
- ...{
- Response. Redirect ("Post. aspx?PID= "+ Id );
- }
- }
- Catch ...{
- }
- }
- Introduction to ASP. NET Applications
- ASP. NET pre-compiled applications
- Overview ASP. NET 2.0 FormView controls
- Optimize ASP. NET 2.0 Profile Provider
- Analysis on ASP. NET Process Model configuration