立即注册! 找回密码?

用户名

密  码

您所在的位置: 首页 > 软件论坛 > 编程开发平台论坛 > 程序员话题论坛 > asp.net技巧:利用模版生成html页
版主: 暂无 版副: 暂无 嘉宾: 暂无
返回列表
浏览:6870 回复:19 asp.net技巧:利用模版生成html页 [复制本帖链接]
  • 用户名:newaspt
  • 头 衔:太平洋舰队上尉
  • 积 分:1641
  • 贡 献:1052
  • 精 华:22
  • 注 册:2007-06-14
本主题由 不败剑圣 于 2007-10-01 08:52:21 设为精华2
asp.net 生成静态页
环境: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>
biaoti
<br>
content<br>
author
</body>
</html> 
分享到: QQ空间 新浪微博 腾讯微博 更多
  • 用户名:小雪.
  • 头 衔:太平洋舰队中尉
  • 积 分:1109
  • 贡 献:539
  • 精 华:4
  • 注 册:2006-03-13
asp不熟悉的说
  • 用户名:eplanet
  • 头 衔:太平洋舰队新兵
  • 积 分:19
  • 贡 献:3
  • 精 华:0
  • 注 册:2006-03-15
PHP 的 Smarty比这个爽多了,会自动根据模板生成静态缓冲页面,并且会自动对这些缓冲进行管理。
  • 用户名:gods110
  • 头 衔:太平洋舰队新兵
  • 积 分:1
  • 贡 献:2
  • 精 华:0
  • 注 册:2007-09-26
Re:[newaspt,1楼]
以下是引用 newaspt 在1楼的发言:
asp.net 生成静态页
环境: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>
biaoti
<br>
content<br>
author
</body>
</html> 


  • 用户名:kkkaddd
  • 头 衔:太平洋舰队新兵
  • 积 分:60
  • 贡 献:5
  • 精 华:0
  • 注 册:2005-09-03
Re:[newaspt,1楼]

JSP更爽
  • 用户名:startnet88
  • 头 衔:太平洋舰队新兵
  • 积 分:3
  • 贡 献:5
  • 精 华:0
  • 注 册:2007-09-30
.NET开发的产品我认为比较好,特别是在远程方面,我们的产品全是用.NET来开发的,在互联网上运行,相当快
http://www.startnet.com.cn
  • 用户名:space4
  • 头 衔:太平洋舰队中士
  • 积 分:758
  • 贡 献:303
  • 精 华:0
  • 注 册:2004-10-24
PHP???还不就是用别人开发好的东西用用.真正用到底层又有多少人呢?
.net的东西,大多要自己开发,自己写组件,控件!根本就是2种概念!
不过这东西写得有点...........
  • 用户名:mahw348
  • 头 衔:太平洋舰队新兵
  • 积 分:35
  • 贡 献:3
  • 精 华:0
  • 注 册:2007-04-03
Re:[newaspt,1楼]

初学...
看不大懂
  • 用户名:红辣椒99
  • 头 衔:太平洋舰队新兵
  • 积 分:16
  • 贡 献:19
  • 精 华:0
  • 注 册:2007-09-19
确实还看不明白,学习中。
  • 用户名:beidaceshia
  • 头 衔:太平洋舰队新兵
  • 积 分:-402
  • 贡 献:84
  • 精 华:0
  • 注 册:2007-11-06
用户被封,内容屏蔽
返回列表

浏览过的板块

PConline联想家庭云中心 终结者B B520 B320 B325