asp.net生成静态html页面,asp.net生成静态页面方法详细说明

最常用的方法从文件读取模版,替换模版中的参数后输出文件,这种方法的生成速度上比第一种要快许多,而且模版内容可以用工具任意编辑

主要代码:

using system;

using system.collections;

using system.componentmodel;

using system.data;

using system.drawing;

using system.web;

using system.web.sessionstate;

using system.web.ui;

using system.web.ui.webcontrols;

using system.web.ui.htmlcontrols;

using system.io;

using system.text;

namespace xinxi

{

///

/// createpage的摘要说明。

///

// www.111cn.net

// 此类是生成静态网页的小程序

public class create

{

public void createpage()

{

}

public static bool writefile(string strtext,string strcontent,string strauthor)

{

string path = httpcontext.current.server.mappath("/test/");//文件输出目录

encoding code = encoding.getencoding("gb2312");

// 读取模板文件

string temp = httpcontext.current.server.mappath("/template/test.html");//模版文件

streamreader sr=null;

streamwriter sw=null;

string str="";

try

{

sr = new streamreader(temp,code);

str = sr.readtoend(); // 读取文件

}

catch(exception exp)

{

httpcontext.current.response.write(exp.message);

httpcontext.current.response.end();

sr.close();

}

string htmlfilename=datetime.now.tostring("yyyymmddhhmmss") ".html";//静态文件名

// 替换内容

// 这时,模板文件已经读入到名称为str的变量中了

str = str.replace("showarticle",strtext); //模板页中的showarticle

str = str.replace("biaoti",strtext);

str = str.replace("content",strcontent);

str = str.replace("author",strauthor);

// 写文件

try

{

sw = new streamwriter(path htmlfilename , false, code);

sw.write(str);

sw.flush();

}

catch(exception ex)

{

httpcontext.current.response.write(ex.message);

httpcontext.current.response.end();

}

finally

{

sw.close();

}

return true;

}

}

}

//原理是利用system.io中的类读写模板文件,然后用replace替换掉模板中的标签,写入静态html

下面看个实例

模板

showarticle

biaoti

content

author

biaoti

content

author

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值