• [编程开发]asp.net生成静态html页面并分别按年月目录存放.

  • 网网网络  阅读[0]次  评论[0]条  发布时间:2009-10-17  发布人:网网网络

文章摘要:asp.net生成静态html页面并分别按年月目录存放.

 一说到新闻系统的话,一定会谈到静态页面生成的,因为静态页面不但是读取速度快,而且又安全;

  静态页面的生成不管是小到现在的企业网站大至网易,qq等门户都用到了;

  那么我们如何来生成静态页呢?

  以什么方式生成静态页面呢……

  在生成静态页面的时候有那些是要注意的呢:

  静态页面命名

  统一存放目录

  静态页面模板

  页面生成

  一般来说,在原来新闻系统的基础上我们可以根据get此页面请求的内容再生成(比如:http;//www.test.com/news.aspx?id=1,get此页面代码直接写至一个文本文件并以html命名即可);

  在这里我所采用的是模板生成,先用dw做一个网页模板,将标题,内容等将要动态实现的内容先以$title$等替换,等在生成的时候替换成新闻的内容;

  命名:在生成的时候一般来说是采用新闻发布的时间转换成的字符串,这个是不会重复的

  另外我还按年份月份把这些静态文件存放在不同的目录,以便于管理,

  在这里根据一个新闻的id调用方法writenews()给定参数id,它就会根据此id从数据库中读取内容,再根据静态模板页面html/test.html生成新的静态页面存放在相应年份月份的目录

  好了,下面是代码:

  using system;

  using system.io;

  using system.web;

  using system.text;

  namespace powerleader.components

  …{

  /**//// 〈summary〉

  /// writetohtml 的摘要说明。

  /// 〈/summary〉

  public class writetohtml

  …{

  public writetohtml()

  …{

  //

  // todo: 在此处添加构造函数逻辑

  //

  }

  public static void writenews(int id)

  …{

  news news = new news();

  news.newsdetails newsdetails = new powerleader.components.news.newsdetails();

  newsdetails = news.getnews(id);

  bool flag;

  flag = writefile(newsdetails);

  }

  public static bool writefile(news.newsdetails newsdetails)

  …{

  directory.createdirectory(httpcontext.current.server.mappath("/powerleader/html/"+newsdetails.addtime.tostring("yyyy")+"/"+newsdetails.addtime.tostring("mm")));

  string path = httpcontext.current.server.mappath("/html/"+newsdetails.addtime.tostring("yyyy")+"/"+newsdetails.addtime.tostring("mm")+"/");

  encoding code = encoding.getencoding("gb2312");

  // 读取模板文件

  string temp = httpcontext.current.server.mappath("/html/text.html");

  streamreader sr = null;

  streamwriter sw = null;

  string stringtempcode = "";

  try

  …{

  sr = new streamreader(temp, code);

  stringtempcode = sr.readtoend(); // 读取文件

  }

  catch(exception exp)

  …{

  httpcontext.current.response.write(exp.message);

  httpcontext.current.response.end();

  sr.close();

  }

  string htmlfilename = newsdetails.addtime.tostring("yyyymmddhhmmss") + ".html";

  // 替换内容

  // 这时,模板文件已经读入到名称为str的变量中了

  stringtempcode = stringtempcode.replace("$pagetitle$","抗战online官方网站…");

  stringtempcode = stringtempcode.replace("$type$",newsdetails.type.tostring().trim());

  stringtempcode = stringtempcode.replace("$author$",newsdetails.author.tostring().trim());

  stringtempcode = stringtempcode.replace("$from$",newsdetails.from.trim());

  stringtempcode = stringtempcode.replace("$time$",newsdetails.addtime.tostring().trim());

  stringtempcode = stringtempcode.replace("$title$",newsdetails.title.trim());

  stringtempcode = stringtempcode.replace("$content$",newsdetails.content);

  // 写文件

  try

  …{

  sw = new streamwriter(path + htmlfilename , false, code);

  sw.write(stringtempcode);

  sw.flush();

  }

  catch(exception ex)

  …{

  httpcontext.current.response.write(ex.message);

  httpcontext.current.response.end();

  }

  finally

  …{

  sw.close();

  }

  return true;

  }

  }

  }
顶一下
(1000)
50%
踩一下
(1000)
50%
【已有0位网友发表了看法,点击查看。】 复制本页共享msn或qq等好友
  • 发表人:      可输入字数[200]字
  • 验证码:验证码,看不清楚?请点击刷新验证码  请输入计算结果
  • 法律声明:网友评论仅供网友表达个人看法,并不表明网网网络同意其观点或证实其描述
48小时评论排行
    友情链接