立即注册! 找回密码?

用户名

密  码

您所在的位置: 首页 > 软件论坛 > 编程开发平台论坛 > 程序员话题论坛 > asp.net技巧:利用模版生成html页
版主: 暂无 版副: 暂无 嘉宾: 暂无
返回列表
浏览:6871 回复:19 asp.net技巧:利用模版生成html页 [复制本帖链接]
  • 用户名:華仔\joshua
  • 头 衔:太平洋舰队新兵
  • 积 分:32
  • 贡 献:35
  • 精 华:0
  • 注 册:2007-12-20
初学...
看不大懂 !
  • 用户名:星海蓝月
  • 头 衔:太平洋舰队新兵
  • 积 分:33
  • 贡 献:84
  • 精 华:0
  • 注 册:2008-04-06
看了 谢谢分享
  • 用户名:weisheng820
  • 头 衔:太平洋舰队新兵
  • 积 分:1
  • 贡 献:2
  • 精 华:0
  • 注 册:2008-06-10
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> 

  • 用户名:wly365
  • 头 衔:太平洋舰队新兵
  • 积 分:99
  • 贡 献:49
  • 精 华:0
  • 注 册:2008-12-02
精妙哦
但是我看得不是很懂
可以给一点提示吗
  • 用户名:kaka_nil
  • 头 衔:太平洋舰队上将
  • 积 分:7712
  • 贡 献:4412
  • 精 华:4
  • 注 册:2007-03-29
这个例子只要对HTML熟悉就行 
  不过这代码不能编译
  • 用户名:kaibb
  • 头 衔:太平洋舰队少校
  • 积 分:2875
  • 贡 献:1473
  • 精 华:1
  • 注 册:2004-02-03
 用php一样可以模板输出静态页的
  • 用户名:zzlovepisces
  • 头 衔:太平洋舰队新兵
  • 积 分:325
  • 贡 献:22
  • 精 华:0
  • 注 册:2009-03-22

头大
  • 用户名:lzhongx
  • 头 衔:太平洋舰队中校
  • 积 分:3232
  • 贡 献:1991
  • 精 华:4
  • 注 册:2009-06-05
学习
  • 用户名:jianxun
  • 头 衔:太平洋舰队新兵
  • 积 分:3
  • 贡 献:3
  • 精 华:0
  • 注 册:2009-08-27
Re:[newaspt,1楼]
ASP还是爽啊
  • 用户名:嗨呦喂
  • 头 衔:太平洋舰队新兵
  • 积 分:3
  • 贡 献:3
  • 精 华:0
  • 注 册:2011-12-02
PHP就别提了,虽然你写代码的时候比较省事,但是机器理解你的代码要多转个弯。因为你用的方法是别人编好的。
直接file_get_contents可以读出文件内容,谁知道cpu是怎么读出来的?
返回列表

浏览过的板块

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