Main Code for converting Asp. Net Dynamic pages to static pages

Source: Internet
Author: User

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 ...{
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.