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