天天看點

三:了解Page類的運作機制(例:在render方法中生成靜态檔案)

我這裡隻寫幾個常用的事件

1.OnPreInit:此事件後将加載個性化資訊和主題

2.OnInit:初始化頁面中伺服器控件的預設值但控件的狀态沒有加載,沒有建立控件樹

3.OnPreLoad:控件完成狀态和回傳資料的加載

4.Page_Load:此事件是在OnInit中訂閱的

5.Render:呈現最終頁面的内容

假設有一個文章資料庫

以前都是通過article.aspx?id=123的動态形式通路的

現在我們想要減輕伺服器壓力,把文章生成靜态檔案

先看article.aspx的程式

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Text;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.IO;//StringWriter名稱空間

namespace _1

{

    public partial class article : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            if(!string.IsNullOrEmpty(Request["id"]))

            Label1.Text = "文章内容為:"+ Request["id"].ToString();

        }

        protected override void Render(HtmlTextWriter writer)

            StringWriter sw = new StringWriter();//這個和StringBuilder沒太大差別

            HtmlTextWriter htmlw = new HtmlTextWriter(sw);

            base.Render(htmlw);//不用傳遞進來的writer

            htmlw.Flush();

            htmlw.Close();

            string PageContent = sw.ToString();

            string path = Server.MapPath("~/Article/");

            string pageurl = xland.MyModule.GetFileName(HttpContext.Current);

            using (StreamWriter stringWriter = File.AppendText(path + pageurl))

            {

                stringWriter.Write(PageContent);

            }

            Response.Write(PageContent);

    }

}

我們還是通過自定義httpModules來實作url重寫

webconfig檔案沒有太大變化

<?xml version="1.0"?>

<configuration>

    <system.web>

        <compilation debug="true"></compilation>

    <httpModules>

      <add name="myModule" type="xland.MyModule" />

    </httpModules>

    </system.web>

</configuration>

MyModule程式

using System.Collections.Generic;

using System.Web;//引用web命名空間

using System.IO;

namespace xland

    public class MyModule:IHttpModule

        public void Init(HttpApplication context)

            context.BeginRequest +=new EventHandler(context_BeginRequest);

        public void context_BeginRequest(object sender, EventArgs e)

            HttpApplication application = (HttpApplication)sender;

            HttpContext context = application.Context;

            //AppRelativeCurrentExecutionFilePath這裡不包括傳過來的參數

            if (context.Request.AppRelativeCurrentExecutionFilePath.ToLower().EndsWith(".aspx"))

                string fileurl = "~/article/" + GetFileName(context);

                if (File.Exists(context.Server.MapPath(fileurl)))

                {

                    context.RewritePath(fileurl, false);

                }

        public static string GetFileName(HttpContext context)

            return context.Request.AppRelativeCurrentExecutionFilePath.ToLower().Replace(".aspx", "").Replace("~/", "") + context.Request.Url.Query.Replace("?id=", "_") + ".html";

        public void Dispose() { }

注釋就不多寫了,相信大家能看懂

這個示例程式隻是為了說明page類的Render事件

如果要用到項目中,請慎重

因為會造成大量的伺服器IO

而且這也不是生成靜态頁面的最佳方案