最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
asp.net生成静态页面方法详细说明
时间:2022-06-25 05:03:02 编辑:袖梨 来源:一聚教程网
最常用的方法从文件读取模版,替换模版中的参数后输出文件,这种方法的生成速度上比第一种要快许多,而且模版内容可以用工具任意编辑
主要代码:
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.111com.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
c代码
public static bool writefile(string strtext,string strcontent,string strauthor)
{
string path = httpcontext.current.server.mappath("/news/");
encoding code = encoding.getencoding("gb2312");
// 读取模板文件
string temp = httpcontext.current.server.mappath("/news/text.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;
}
相关文章
- 天堂漫画官网入口-首页官方正版直连 天堂漫画官网首页-正版授权直达 12-14
- 巅峰极速日服纯净官网入口-极速日服官方正版入口 12-14
- 四六级考试官网登录通道-全国大学英语四六级报名系统 12-14
- 1元10抖币极速充值官网入口-抖币充值iOS用户独享通道 12-14
- 学习通网页版登录入口 官方免费在线使用学校通 12-14
- 126免费邮箱官网密码找回通道-126免费邮箱官网双重验证入口 12-14