asp教程.net生成静态页面代码
private static void createfile(string userid, string filename, string htmlcode)
{
filename += ".html";
string localpath = "e:/www.111com.net/sun/sundecorativesystem/users/" + userid;
if (!directory.exists(localpath))
{
directory.createdirectory(localpath);
}
localpath += "/" + filename;
if (file.exists(localpath))
{
file.delete(localpath);
}
filestream fs = new filestream(localpath, filemode.createnew);
streamwriter sw = new streamwriter(fs);
sw.autoflush = true;
htmlcode = htmlcode.replace("+", """);
sw.write(htmlcode);
sw.flush();
sw.close();
fs.close();}
//下面来看一款完整理的asp.net教程生成html代码实例吧。
环境:microsoft .net framework sdk v1.1
os:windows server 2003 中文版
asp.net生成静态html页
在asp中实现的生成静态页用到的filesystemobject对象!
在.net中涉及此类操作的是system.io
以下是程序代码 注:此代码非原创!参考别人代码
//生成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代码
-------------------------------------------------------------------------
showarticle
biaoti
content
author