|
|
|
asp.net生成静态页 |
|
|
| http://www.iyit.net 日期:2005-11-24 17:31:08 来源:易特网络技术 点击: |
//生成html页
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;

此函数放在conn.cs基类中了
在添加新闻的代码中引用 注:工程名为hover

if(hover.conn.writefilethis.title.text.tostring),this.content.text.tostring),this.author.text.tostring)))
 {
response.write("添加成功");
}
else
 {
response.write("生成html出错!");
}


模板页text.html代码


code:
<!doctype html public "-//w3c//dtd html 4.0 transitional//en" >
<html>
<head>
<title>showarticle</title>
<body>
biaoti
<br>
content<br>
author
</body>
</html>
biaoti
<br>
content<br>
author
</body>
</html>
|
上一篇:根据数据库生成xml二法
下一篇:顺序求出c(n,r)的排列组合
[发送给好友] [打印本页] [关闭窗口] [返回顶部] 转载请注明来源:http://www.iyit.net |
|
| 特别声明: 本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题请联系编辑人员,我们尽快予以更正。 |
| 责任编辑: |
投稿作者: 易特网络技术 |
| 信息来源: 易特网络技术 |
录入时间: 2005-11-24 17:31:08 |
| 浏览次数: |
投稿信箱: shtghy@163.com |
|
|
|
|
|