您的位置:首页 > 编程语言 > ASP

ASP.NET生成静态页面实现方法

2010-10-01 22:18 639 查看
string mbPath = Server.MapPath("template.htm");
Encoding code = Encoding.GetEncoding("gb2312");

StreamReader sr = null;
StreamWriter sw = null;

string str = null;

//读取
try
{
sr = new StreamReader(mbPath, code);
str = sr.ReadToEnd();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
sr.Close();
}

//根据时间自动重命名,扩展名也可以自行修改
string filename = DateTime.Now.ToString("yyyyMMddHHmmss") + ".htm";

str = str.Replace("$title$",this.TextBox1.Text); //替换标题
str = str.Replace("$author$",this.TextBox2.Text); //作者
str = str.Replace("$addtime$",this.TextBox3.Text); //发布时间
str = str.Replace("$content$",this.TextBox4.Text);//内容

try
{
sw = new StreamWriter(Server.MapPath("html/") + filename, false, code);
sw.Write(str);
sw.Flush();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
sw.Close();
Response.Write("恭喜<a href="html/" mce_href="html"" + filename + " target=_blank>" + filename + "</a>已经生成,保存在html文件夹下!");
}

ASP.NET生成静态页面实现方法
<!--Main.Aspx-->
<%@ page language="C#" %>
<%@ import namespace=System.IO %>
<mce: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;
  }
}
// --></mce:script>

<!--Main_Execute.aspx-->
<%@ page language="C#" %>
<html>
<head runat="server">
  <title>Untitled Page</title>
</head>
<body>
ID:
<%=Request.QueryString["id"]%>
</body>
</html>
  <!--Main.Aspx-->
<%@ page language="C#" %>
<%@ import namespace=System.IO %>
<mce: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;
  }
}
// --></mce:script>

<!--Main_Execute.aspx-->
<%@ page language="C#" %>
<html>
<head runat="server">
  <title>Untitled Page</title>
</head>
<body>
ID:
<%=Request.QueryString["id"]%>
</body>
</html>

  其中原理是这样的。

  Main_Execute.aspx是生成HTML的页面。

  现在用Main.aspx来对它进行缓存.

  过程如下:

  首先根据页面参数算出文件名。(这个例子只根据Request.QueryString["id"]来算)

  尝试读取缓存的文件.如果成功,那么Response.End();

  如果不成功:

  使用Server.Execute来调用Main_Execute.aspx,并且获取它的结果内容。

  得到内容后,立刻输出到客户端。

  最后把内容写进文件里,提供给下一次做为缓存度取。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: