前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >三:理解Page类的运行机制(例:在render方法中生成静态文件)

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

作者头像
liulun
发布2022-05-08 17:55:56
3470
发布2022-05-08 17:55:56
举报
文章被收录于专栏:liulunliulun

我这里只写几个常用的事件 1.OnPreInit:此事件后将加载个性化信息和主题 2.OnInit:初始化页面中服务器控件的默认值但控件的状态没有加载,没有创建控件树 3.OnPreLoad:控件完成状态和回传数据的加载 4.Page_Load:此事件是在OnInit中订阅的 5.Render:呈现最终页面的内容 假设有一个文章数据库 以前都是通过article.aspx?id=123的动态形式访问的 现在我们想要减轻服务器压力,把文章生成静态文件 先看article.aspx的程序

代码语言:javascript
复制
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文件没有太大变化

代码语言:javascript
复制
<?xml version="1.0"?>
<configuration>
    <system.web>
        <compilation debug="true"></compilation>
    <httpModules>
      <add name="myModule" type="xland.MyModule" />
    </httpModules>
    </system.web>
</configuration>

MyModule程序

代码语言:javascript
复制
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 而且这也不是生成静态页面的最佳方案

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2009-01-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档