• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
思想的思想
很久很久以前,谎言和真实在河边洗澡,谎言先洗好,穿了真实的衣服离开, 真实却不肯穿谎言的衣服。后来,在人们的眼里,只有穿着真实衣服的谎言,却很难接受赤裸裸的真实
博客园    首页    新随笔    联系   管理    订阅  订阅

ASP.NET 静态页生成

Code:
//生成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>  

posted @ 2008-01-15 09:44  混世魔王  阅读(214)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3