惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
V2EX
人人都是产品经理
人人都是产品经理
WordPress大学
WordPress大学
博客园 - Franky
小众软件
小众软件
酷 壳 – CoolShell
酷 壳 – CoolShell
Apple Machine Learning Research
Apple Machine Learning Research
爱范儿
爱范儿
IT之家
IT之家
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
Visual Studio Blog
S
SegmentFault 最新的问题
美团技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
有赞技术团队
有赞技术团队
B
Blog RSS Feed
Last Week in AI
Last Week in AI
Jina AI
Jina AI
博客园 - 司徒正美
The Cloudflare Blog
博客园_首页
博客园 - 聂微东
宝玉的分享
宝玉的分享
大猫的无限游戏
大猫的无限游戏

博客园 - TONYBINLJ

.Net架构必备工具列表 .NET开源项目 10个经典的Android开源项目 【操作命令】 代码検索 MD5,SHA1,SHA256,NTLM,LM等Hash在线破解网站收集 用jQuery合并表格中相同文本的相邻单元格 做销售不得不看的20部电影 据说月薪过了6000的应届生,都知道这些网站 给力的2011,我来了 jqGrid属性中文详细说明 推荐全球最大图标搜索FindIcons.com jQuery ajax调用: $.get $.post $.getJSON 详解 C#读图片EXIF信息 SQLServer常见查询问题 网站开发人员应该知道的61件事 用例点公式 制定了一个提高计划 希望指点 收集脚本SQL
三:理解Page类的运行机制(例:在render方法中生成静态文件)
TONYBINLJ · 2010-03-30 · via 博客园 - TONYBINLJ

我这里只写几个常用的事件
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
而且这也不是生成静态页面的最佳方案