ASP.NET生成静态页面实现办法[网站编程]
本文“ASP.NET生成静态页面实现办法[网站编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
以下是引用片段:
<!--Main.ASPx-->
<%@ page language="C#" %>
<%@ import namespace=System.IO %>
<script runat="server">
protected override void OnInit (EventArgs e)
{
int id;
try
{
id = int.Parse (Request.QueryString["id"]);
}
catch
{
throw (new Exception ("页面没有指定id"));
}
string filename=Server.MapPath("statichtml_" id ".html");
//尝试读取已有文件
Stream s = GetFileStream (filename);
if (s != null)//假如文件存在并且读取成功
{
using (s)
{
Stream2Stream (s, Response.OutputStream);
Response.End ();
}
}
//调用Main_Execute,并且获得其输出
StringWriter sw = new StringWriter ();
Server.Execute ("Main_Execute.aspx", sw);
string content = sw.ToString ();
//输出到客户端
Response.Write(content);
Response.Flush();
//写进文件
try
{
using (FileStream fs = new FileStream (filename, FileMode.Create, FileAccess.Write, FileShare.Write))
{
using (StreamWriter streamwriter = new StreamWriter (fs, Response.ContentEncoding))
{
streamwriter.Write (content);
}
}
}
finally
{
//Response.End ();
}
}
static public void Stream2Stream (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);
}
}
public Stream GetFileStream(string filename)
{
try
{
DateTime dt = File.GetLastWriteTime (filename);
TimeSpan ts=dt - DateTime.Now;
if(ts.TotalHours>1)
return null; //1小时后过期
return new FileStream (filename, FileMode.Open, FileAccess.Read, FileShare.Read);
}
软件开辟网
catch
{
return null;
}
}
</script>
<!--Main_Execute.aspx-->
<%@ page language="C#" %>
<html>
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
ID:
<%=Request.QueryString["id"]%>
</body>
</html>
以上是“ASP.NET生成静态页面实现办法[网站编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |