三:理解Page类的运行机制(例:在render方法中生成静态文件)

时间:2024-04-12 10:34:24

我这里只写几个常用的事件
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;
using System.Collections.Generic;
using System.Web;//引用web命名空间
using System.Text;
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
而且这也不是生成静态页面的最佳方案