ASP.Net生成静态HTML页!
点击次数:18 次 发布日期:2008-11-26 10:59:05 作者:源代码网
|
源代码网推荐 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文件 源代码网推荐 而且需要把生成的文件名等写入数库以便以后调用等,此实例只是实现了根据提交过来参数替换模板中的相应的字段! 需要完善的地方很多!哪位有高见,欢迎赐教! 源代码网推荐 源代码网推荐 做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。 源代码网推荐 源代码网供稿. |
