日期:2014-05-19  浏览次数:20420 次

求生成静态页思路,无限分类(Demo)或思路!
因为公司要做一个新闻系统,要用到生成静态页面,和无限分类!在网上查了一下相关资料,生成静态页有url重写和用模板!现在也不知道这个新闻系统到底会有多少数据,不知道用url重写会不会太影响速度!因为每个新闻都有评论,每次评论得从新生成静态页!请大家提供点思路或给点demo,偶以前从没做过这两样东东。先送上200分,分不够在开帖给。先谢!

------解决方案--------------------
顶上,期待中....
------解决方案--------------------
SF
------解决方案--------------------
js http://www.chinageren.com/jc/HTML/86173.html 我们公司网站也是这么做的
------解决方案--------------------
<!--Main.Aspx-->
<%@ page language= "C# " %>
<%@ import namespace=System.IO %>
<script runat= "server ">
protected override void OnInit (EventArgs e)
{
  int id;
  try
  {
    id = int.Parse (Request.QueryString[ "id "]);
  }
  catch
  {
    throw (new Exception ( "页面没有指定id "));
  }
 
  string filename=Server.MapPath( "statichtml_ "+id+ ".html ");
 
  //尝试读取已有文件
  Stream s = GetFileStream (filename);
  if (s != null)//如果文件存在并且读取成功
  {
    using (s)
    {
      Stream2Stream (s, Response.OutputStream);
      Response.End ();
    }
  }
 
 
  //调用Main_Execute,并且获取其输出
  StringWriter sw = new StringWriter ();
  Server.Execute ( "Main_Execute.aspx ", sw);
 
  string content = sw.ToString ();
 
  //输出到客户端
  Response.Write(content);
  Response.Flush();
 
  //写进文件
 
  try
  {
    using (FileStream fs = new FileStream (filename, FileMode.Create, FileAccess.Write, FileShare.Write))
    {
      using (StreamWriter streamwriter = new StreamWriter (fs, Response.ContentEncoding))
      {
        streamwriter.Write (content);
      }
    }
  }
  finally
  {
    //Response.End ();
  }
}
static public void Stream2Stream (Stream src, Stream dst)
{
  byte[] buf = new byte[4096];
  while (true)
  {
    int c = src.Read (buf, 0, buf.Length);
    if(c==0)
      return;
    dst.Write (buf, 0, c);
  }
}
public Stream GetFileStream(string filename)
{
  try
  {
    DateTime dt = File.GetLastWriteTime (filename);
    TimeSpan ts=dt - DateTime.Now;
    if(ts.TotalHours> 1)
      return null;    //1小时后过期
    return new FileStream (filename, FileMode.Open, FileAccess.Read, FileShare.Read);
  }
  catch
  {
    return null;
  }
}
</script>


<!--Main_Execute.aspx-->
<%@ page language= "C# " %>
<html>
<head runat= "server ">
  <title> Untitled Page </title>
</head>
<body>

ID:
<%=Request.QueryString[ "id "]%>

</body>
</html>


其中原理是这样的.
Main_Execute.aspx是生成HTML的页面.

现在用Main.aspx来对它进行缓存.
过程如下:

首先根据页面参数算出文件名.(这个例子只根据Request.QueryString[ "id "]来算)
尝试读取缓存的文件.如果成功,那么Response.End();
如果不成功:
使用Server.Execute来调用Main_Execute.aspx,并且获取它的结果内容.
得到内容后,立刻输出到客户端.
最后把内容写进文件里,提供给下一次做为缓存度取.


------解决方案--------------------
URL重写好,方便……
------解决方案--------------------
mark一下
------解决方案--------------------