首页 新闻 赞助 找找看

利用Render 静态化页面,谁搞过,这样性能如何???

0
[已关闭问题] 关闭于 2012-04-25 13:20

源码:

t.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="t.aspx.cs" Inherits="t" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
sdfasdfsadfsdafs 测试
</body>
</html>

t.aspx.cs

using System;
using System.IO;
using System.Web.UI;

public partial class t : System.Web.UI.Page
{
    string CachePath;
    bool BeCreateCacheFile = false;
    protected void Page_Load(object sender, EventArgs e)
    {
        string RelativePath ="/RunTime/Index.html"; 
        CachePath = Server.MapPath(RelativePath);

        FileInfo Info = new FileInfo(CachePath);

        if (Info.Exists)
        {
      

            if ((DateTime.Now-Info.LastWriteTime).Seconds>10)
            {
                BeCreateCacheFile = true;
            }
            else
            {
                BeCreateCacheFile = false;
                Response.Write("由缓存输出<br />");
 
                Response.TransmitFile(RelativePath);
            }
        }
        else
        {
            BeCreateCacheFile = true;
        }

    }




    protected override void Render(HtmlTextWriter htmlTextWriter)
    {

        if (BeCreateCacheFile)
        {
            Response.Write("<span style=\"color:#f00\">写入缓存</span><br />");
            StringWriter stringWriter = new StringWriter();
            HtmlTextWriter newHtmlTextWriter = new HtmlTextWriter(stringWriter);
            base.Render(newHtmlTextWriter);

            string htmlContent = stringWriter.ToString();

            lock (this)
            {
                using (FileStream stream = File.Create(CachePath))
                {
                    byte[] data = System.Text.Encoding.UTF8.GetBytes(htmlContent);
                    stream.Write(data, 0, data.Length);
                    stream.Flush();
                    stream.Close();
                }
            }
            stringWriter.Close();
            htmlTextWriter.Write(htmlContent);
        }
    }
}
fun5的主页 fun5 | 初学一级 | 园豆:4
提问于:2012-04-19 11:59
< >
分享
所有回答(2)
0

最好的就是用上面这位兄弟说的先写一个模板, 然后通过模板上的标志替换成相应的数据。

_大师兄_ | 园豆:174 (初学一级) | 2012-04-19 16:09

这种最操蛋

支持(0) 反对(0) fun5 | 园豆:4 (初学一级) | 2012-04-19 18:33
0
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.UI;
 6 using System.Web.UI.WebControls;
 7 using System.Text;
 8 
 9 public partial class webControl_hot_news_list : System.Web.UI.UserControl
10 {
11     private string filePath = "../webControl/temp/hot_news_list.ascx";
12 
13     /// <summary>
14     /// 获取导航信息
15     /// </summary>
16     /// <param name="top"></param>
17     /// <returns></returns>
18     protected string GetTopNewsList(int top)
19     {
20         Mydream.BLL.News.NewsBLL newsBLL = new Mydream.BLL.News.NewsBLL();
21         IList<Mydream.Model.IDValue> list = newsBLL.GetNewslist(top);
22         StringBuilder sb = new StringBuilder();
23         int i=1;
24         foreach (Mydream.Model.IDValue idValue in list)
25         {
26             sb.Append("<li>");
27             sb.AppendFormat("<img src=\"images/web/top/p{0}.gif\" alt=\"{0}\" />", i);
28             sb.Append("&nbsp;&nbsp;");
29             sb.AppendFormat("<a href=\"news_{0}.html\" title=\"{1}\">{2}</a>", idValue.ID, idValue.Value, Mydream.Common.StrTool.StringUtity.SubStr(idValue.Value, 8, "..."));
30             sb.Append("</li>");
31             i++;
32         }
33         return sb.ToString();
34     }
35 
36     protected override void Render(HtmlTextWriter writer)
37     {
38         bool Redirect = false;
39         if (Request["File"] == null) { Redirect = true; }
40         Render(writer, filePath, Redirect);
41     }
42 
43     /// <summary>
44     /// 生成静态方法
45     /// </summary>
46     /// <param name="writer"></param>
47     /// <param name="filePath"></param>
48     /// <param name="redirect"></param>
49     public void Render(HtmlTextWriter writer, string filePath, bool redirect)
50     {
51         System.IO.StringWriter html = new System.IO.StringWriter();
52         System.Web.UI.HtmlTextWriter tw = new System.Web.UI.HtmlTextWriter(html);
53         base.Render(tw);
54         System.IO.StreamWriter sw;
55         sw = new System.IO.StreamWriter(Server.MapPath(this.filePath), false, System.Text.Encoding.GetEncoding("utf-8"));
56         sw.Write(html.ToString());
57         sw.Close();
58         tw.Close();
59     }
60 }

(适用)我页面数据不是因为用户而会修改,只会后台管理员修改,导致数据改变就执行一次,这样就避免每次都访问数据库,方法和你的有点像!只不过我是用户自定义控件,总共2个用户自定义控件,一个是绑定数据等待执行,一个是获取执行后的html标签数据,把着html标签数据拉到网页上

xu_happy_you | 园豆:222 (菜鸟二级) | 2012-04-20 11:47
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册