生成静态页的三种方法

做网站时,有时为了提高性能,除了使用ASP.Net页面缓存以外,也可以将网站首页生成静态页。

生成静态页面的方法有很多,我们这里就介绍两种:
  1. 模板法
  2. 重写Page的Render方法(该方法来自 "孟子E " )
  3. ASP.Net 2.0中的HttpRequest.SaveAs方法。

1.模板法

废话不说,就是经典的实现方法,直接放代码:

None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Drawing;
None.gif
using  System.Web;
None.gif
using  System.Web.SessionState;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
using  System.IO;
None.gif
using  System.Text;
None.gif
None.gif
namespace  menutest
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// Conn 的摘要说明。
ExpandedSubBlockEnd.gif  
/// </summary>

InBlock.gif  // by kyo
InBlock.gif  
// 此类是生成静态网页的小程序  
InBlock.gif
  public class Conn
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
public Conn()
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif   
ExpandedSubBlockEnd.gif   }

InBlock.gif   
public static bool WriteFile(string strText,string strContent,string strAuthor)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
string path = HttpContext.Current.Server.MapPath("/menutest/");
InBlock.gif    Encoding code 
= Encoding.GetEncoding("gb2312");
InBlock.gif    
// 读取模板文件
InBlock.gif
    string temp = HttpContext.Current.Server.MapPath("/menutest/news/text.html");
InBlock.gif    StreamReader sr
=null;
InBlock.gif    StreamWriter sw
=null;
InBlock.gif    
string str=""
InBlock.gif    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     sr 
= new StreamReader(temp,code);
InBlock.gif     str 
= sr.ReadToEnd(); // 读取文件
ExpandedSubBlockEnd.gif
    }

InBlock.gif    
catch(Exception exp)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     HttpContext.Current.Response.Write(exp.Message);
InBlock.gif     HttpContext.Current.Response.End();
InBlock.gif     sr.Close();
ExpandedSubBlockEnd.gif    }

InBlock.gif 
InBlock.gif 
InBlock.gif    
//string htmlfilename=DateTime.Now.ToString("yyyyMMddHHmmss")+".html";
InBlock.gif
    string htmlfilename="kyo.html";
InBlock.gif    
// 替换内容
InBlock.gif    
// 这时,模板文件已经读入到名称为str的变量中了
InBlock.gif
    str = str.Replace("ShowArticle",strText); //模板页中的ShowArticle
InBlock.gif
    str = str.Replace("biaoti",strText);
InBlock.gif    str 
= str.Replace("content",strContent);
InBlock.gif    str 
= str.Replace("author",strAuthor);
InBlock.gif    
// 写文件
InBlock.gif
    try
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     sw 
= new StreamWriter(path + htmlfilename , false, code);
InBlock.gif     sw.Write(str);
InBlock.gif     sw.Flush();
ExpandedSubBlockEnd.gif    }

InBlock.gif    
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     HttpContext.Current.Response.Write(ex.Message);
InBlock.gif     HttpContext.Current.Response.End();
ExpandedSubBlockEnd.gif    }

InBlock.gif    
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     sw.Close();
ExpandedSubBlockEnd.gif    }

InBlock.gif    
return true;
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif}

None.gif
// 原理是利用System.IO中的类读写模板文件,然后用Replace替换掉模板中的标签,写入静态html.
None.gif


2.重写Render法

函数重载了Page.Render函数,作用是把页面的HTML文本截下来,保存到文件里,进一步的可以去看一下ASP.Net页面生命过程。

代码如下

None.gif protected   override   void  Render(HtmlTextWriter writer)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    System.IO.StringWriter html 
= new System.IO.StringWriter();
InBlock.gif    System.Web.UI.HtmlTextWriter tw 
= new HtmlTextWriter(html);
InBlock.gif    
base.Render(tw);
InBlock.gif    System.IO.StreamWriter sw 
= new System.IO.StreamWriter(Server.MapPath("index.html"), false, System.Text.Encoding.Default);
InBlock.gif    sw.Write(html.ToString());
InBlock.gif    sw.Close();
InBlock.gif    tw.Close();
InBlock.gif    Response.Write(
"页面生成成功!");
ExpandedBlockEnd.gif}

将以上上函数数加如到你要生成静太文件的页面里。


3.模板法

在ASP.NET 2.0中,有时候需要对ASP.NET生成的HTML代码进行处理,或者是保存成静态文件。ASP.NET 提供了直接将请求保存成文件的方法:HttpRequest.SaveAs方法。

转载于:https://www.cnblogs.com/rungroo/archive/2007/01/23/628431.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值