关于在AspNet中动态页面转静态页面的方法网上比较多结合实际的需求我在网上找了一些源代码并作修改现在把修改后的代码以及说明写一下
一个是一个页面转换的类该类通过静态函数Changfile()来实现动态页面到静态页面的转换
using System;
using SystemData;
using SystemConfiguration;
using SystemWeb;
using SystemWebSecurity;
using SystemWebUI;
using SystemWebUIWebControls;
using SystemWebUIWebControlsWebParts;
using SystemWebUIHtmlControls;
using SystemText;
using SystemIO;
/**//// <summary>
/// Summary description for HtmlProxy
/// </summary>
public class HtmlProxy
{
public HtmlProxy()
{
}
public static bool ChangeFile(int id)
{
string filename = HttpContextCurrentServerMapPath(Post_ + id + html);
//尝试读取已有文件
Stream st = GetFileStream(filename);
//如果文件存在并且读取成功
if (st != null)
{
using (st)
{
StreamToStream(st HttpContextCurrentResponseOutputStream);
return true;
//ResponseEnd();
}
}
else
{
StringWriter sw = new StringWriter();
HttpContextCurrentServerExecute(ForumDetailaspx?PID= + id sw);
string content = swToString();
//写进文件
try
{
using (FileStream fs = new FileStream(filename FileModeCreate FileAccessWrite FileShareWrite))
{
using (StreamWriter stw = new StreamWriter(fs HttpContextCurrentResponseContentEncoding))
{
stwWrite(content);
}
}
return true;
}
catch { return false; }
}
}
private static Stream GetFileStream(string filename)
{
try
{
DateTime dt = FileGetLastWriteTime(filename);
TimeSpan ts = dt DateTimeNow;
if (tsTotalHours >)
{
//一小时后过期
return null;
}
return new FileStream(filename FileModeOpen FileAccessRead FileShareRead);
}
catch { return null; }
}
static public void StreamToStream(Stream src Stream dst)
{
byte[] buf = new byte[];
while (true)
{
int c = srcRead(buf bufLength);
if (c == )
return;
dstWrite(buf c);
}
}
}
在页面文件中ForURLaspx的后台代码如下
protected void Page_Load(object sender EventArgs e)
{
try
{
int id = intParse(RequestQueryString[PID]);
if(HtmlProxyChangeFile(id))
{
ResponseRedirect(Post_ + id + html);
}
else
{
ResponseRedirect(Postaspx?PID= + id );
}
}
catch {
}
}