ASP.NET实现静态页面方法浅析

ASP.NET实现静态页面的方法是什么呢?首先让我们看看ASP.NET的源码实例:

  1. ﹤!--Main.Aspx--﹥   
  2. ﹤%@ page language="C#" %﹥   
  3. ﹤%@ import namespace=System.IO %﹥   
  4. ﹤script runat="server"﹥   
  5. protected override void OnInit (EventArgs e)   
  6. {   
  7.   int id;   
  8.   try   
  9.   {   
  10.   id = int.Parse (Request.QueryString["id"]);   
  11.   }   
  12.   catch   
  13.   {   
  14.   throw (new Exception ("页面没有指定id"));   
  15.   }   
  16.     
  17.   string filename=Server.MapPath("statichtml_"+id+".html");   
  18.     
  19.   //尝试读取已有文件   
  20.   Stream s = GetFileStream (filename);   
  21.   if (s != null)//如果文件存在并且读取成功   
  22.   {   
  23.   using (s)   
  24.   {   
  25.   Stream2Stream (s, Response.OutputStream);   
  26.   Response.End ();   
  27.   }   
  28.   }   
  29.     
  30.     
  31.   //调用Main_Execute,并且获取其输出   
  32.   StringWriter sw = new StringWriter ();   
  33.   Server.Execute ("Main_Execute.aspx", sw);   
  34.     
  35.   string content = sw.ToString ();   
  36.     
  37.   //输出到客户端   
  38.   Response.Write(content);   
  39.   Response.Flush();   
  40.     
  41.   //写进文件   
  42.     
  43.   try   
  44.   {   
  45.   using (FileStream fs = new FileStream (filename, FileMode.Create, FileAccess.Write, FileShare.Write))   
  46.   {   
  47.   using (StreamWriter streamwriter = new StreamWriter (fs, Response.ContentEncoding))   
  48.   {   
  49.   streamwriter.Write (content);   
  50.   }   
  51.   }   
  52.   }   
  53.   finally   
  54.   {   
  55.   //Response.End ();   
  56.   }   
  57. }   
  58. static public void Stream2Stream (Stream src, Stream dst)   
  59. {   
  60.   byte[] buf = new byte[4096];   
  61.   while (true)   
  62.   {   
  63.   int c = src.Read (buf, 0, buf.Length);   
  64.   if(c==0)   
  65.   return;   
  66.   dst.Write (buf, 0, c);   
  67.   }   
  68. }   
  69. public Stream GetFileStream(string filename)   
  70. {   
  71.   try   
  72.   {   
  73.   DateTime dt = File.GetLastWriteTime (filename);   
  74.   TimeSpan ts=dt - DateTime.Now;   
  75.   if(ts.TotalHours﹥1)   
  76.   return null;  //1小时后过期   
  77.   return new FileStream (filename, FileMode.Open, FileAccess.Read, FileShare.Read);   
  78.   }   
  79.   catch   
  80.   {   
  81.   return null;   
  82.   }   
  83. }   
  84. ﹤/script﹥   
  85.  
  86. ﹤!--Main_Execute.aspx--﹥   
  87. ﹤%@ page language="C#" %﹥   
  88. ﹤html﹥   
  89. ﹤head runat="server"﹥   
  90.   ﹤title﹥Untitled Page﹤/title﹥   
  91. ﹤/head﹥   
  92. ﹤body﹥   
  93. ID:   
  94. ﹤%=Request.QueryString["id"]%﹥   
  95. ﹤/body﹥   
  96. ﹤/html﹥  

其中ASP.NET实现静态页面的原理是这样的。

Main_Execute.aspx是生成HTML的页面。

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

ASP.NET实现静态页面过程如下:

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

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

如果不成功:

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

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

最后把内容写进文件里,提供给下一次做为缓存度取。

ASP.NET实现静态页面的方法就向你介绍到这里,那么关于ASP.NET实现静态页面你是不是多了份了解了呢?

…… 2.asp.net代码:   //---------------------读html模板页面到stringbuilder对象里----   string[] format=new string[4];//定义和htmlyem标记数目一致的数组   StringBuilder htmltext=new StringBuilder();   try   {    using (StreamReader sr = new StreamReader("存放模板页面的路径和页面名"))    {   String line;   while ((line = sr.ReadLine()) != null)   {    htmltext.Append(line);   }   sr.Close();    }   }   catch   {    Response.Write("<Script>alert('读取文件错误')</Script>");   }   //---------------------给标记数组赋值------------   format[0]="background="bg.jpg"";//背景图片   format[1]= "#990099";//字体颜色   format[2]="150px";//字体大小   format[3]= "<marquee>生成的模板html页面</marquee>";//文字说明   //----------替换htm里的标记为你想加的内容   for(int i=0;i<4;i )   {    htmltext.Replace("$htmlformat[" i "]",format[i]);   }   //----------生成htm文件------------------――   try   {    using(StreamWriter sw=new StreamWriter("存放路径和页面名",false,System.Text.Encoding.GetEncoding("GB2312")))   {    sw.WriteLine(htmltext);    sw.Flush();    sw.Close();   }   }   catch   {   Response.Write ("The file could not be wirte:");   }   小结   用此方法可以方便的生成html文件。程序使用了是循环替换,因此对需替换大量元素的模板速度非常快。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值