asp.net dynamic page to static page main code _ Practical skills

Source: Internet
Author: User
Tags httpcontext
One is a page conversion class, which is implemented by static function Changfile () to convert dynamic pages to static pages.
Copy Code code 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");
Attempt to read an existing file Stream st = getfilestream (filename);
If the file exists and the read succeeds
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 in 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)
... {
Expires 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[4096];
while (true)
... {
int c = src. Read (buf, 0, buf. Length);
if (c = = 0)
Return
Dst. Write (buf, 0, C);
}
}
}
In the paging file, the background code for 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.