版权所有,禁止匿名转载;禁止商业使用。
环境: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代码
-------------------------------------------------------------------------
<!doctype html public "-//w3c//dtd html 4.0 transitional//en" > <html> <head> <title>showarticle</title> <body> biaoti <br> content<br> author </body> </html>
------------------------------------------------------------------------
提示添加成功后会出以当前时间为文件名的html文件!上面只是把传递过来的几个参数直接写入了html文件中,在实际应用中需要先添加数据库,然后再写入html文件
而且需要把生成的文件名等写入数库以便以后调用等,此实例只是实现了根据提交过来参数替换模板中的相应的字段! 需要完善的地方很多!