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

推荐订阅源

爱范儿
爱范儿
博客园_首页
W
WeLiveSecurity
S
Secure Thoughts
S
Security @ Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Hugging Face - Blog
Hugging Face - Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
H
Hacker News: Front Page
Project Zero
Project Zero
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
U
Unit 42
N
News and Events Feed by Topic
N
News and Events Feed by Topic
Hacker News - Newest:
Hacker News - Newest: "LLM"
Forbes - Security
Forbes - Security
T
Tor Project blog
I
Intezer
B
Blog
F
Full Disclosure
Security Archives - TechRepublic
Security Archives - TechRepublic
F
Fortinet All Blogs
Schneier on Security
Schneier on Security
T
Threat Research - Cisco Blogs
AI
AI
Google DeepMind News
Google DeepMind News
L
LINUX DO - 最新话题
Cloudbric
Cloudbric
L
Lohrmann on Cybersecurity
WordPress大学
WordPress大学
博客园 - 聂微东
雷峰网
雷峰网
P
Privacy International News Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
PCI Perspectives
PCI Perspectives
Y
Y Combinator Blog
Spread Privacy
Spread Privacy
Simon Willison's Weblog
Simon Willison's Weblog
罗磊的独立博客
Vercel News
Vercel News
A
Arctic Wolf
The Register - Security
The Register - Security
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Microsoft Azure Blog
Microsoft Azure Blog
H
Heimdal Security Blog
Know Your Adversary
Know Your Adversary
P
Proofpoint News Feed
C
Cybersecurity and Infrastructure Security Agency CISA
P
Proofpoint News Feed

博客园 - Tiu

2010走了,又是一年,留个脚印 Commerce Server 2007 随笔一 2009眨眼间过去了,留个纪念 asp.net ajax随笔二 asp.net中慎用static全局变量 asp.net ajax随笔一 收集的关于依赖注入及Unity application block入门的一些资料 2008最后一篇:总结与展望 文件操作类简介 防止页面在提交的过程中多次点击按钮 第一次使用SQLCLR C#网络编程随笔一 装AJAX.NET 1.0的环境,我遇到个问题,进来解答下 关于邮件群发 XML学习一 asp.net窗中的两个Form问题 动态从数据库中选择Top 个数 关于在数据层返回SqlDataReader 编写类和子程序的几个原则
URL重写入门
Tiu · 2006-12-27 · via 博客园 - Tiu

      以前没有接触过这块,现在趁着有时间便看了看,我是从了解HttpModule和HttpHandler开始的,当我们请求.aspx页面时,服务器的IIS服务会把相应的处理交给ASPNET_ISAPI.DLL,而aspnet_isapi.dll会通过一个"管道"传给asp.net相应的进程处理,进入HttpRuntime,这时就有一个HttpApplication Factory的容器接收http请求,并依次进行:HttpModule-HttpApplication Factory-HttpHandler,处理完毕后便会返回,HttpModule 会贯穿始终,只不过会在中间一小块把控制权交给HttpHandler。要应用HttpModule和HttpHandler则必须从IHttpModule和IHttpHandler接口进行实现,http://msdn.microsoft.com/library/chs/default.asp?url=/library/CHS/cpguide/html/cpconHttpModules.asp这里便是msdn对HttpModule的简要说明:

HttpModule 是实现 IHttpModule 接口和处理事件的程序集。ASP.NET 包含一组可由应用程序使用的 HttpModule 模块。例如,ASP.NET 提供了 SessionStateModule 来向应用程序提供会话状态服务。可以创建自定义 HttpModule 以响应 ASP.NET 事件或用户事件。

编写 HttpModule 的一般过程为:

  • 实现 IHttpModule 接口。
  • 处理 Init 方法并为所需事件进行注册。
  • 处理该事件。
  • 如果必须进行清理,还可根据需要实现 Dispose 方法。
  • 在 Web.config 中注册该模块。

在Init里我们可以声明一些委托:下面是我的例子:

        public void Init(HttpApplication application)
        
{
            
//在asp.net响应请求时作为Http执行管线连中的第一个事件发生
            application.BeginRequest+=new EventHandler(application_BeginRequest);
            application.EndRequest
+=new EventHandler(application_EndRequest);//最后一个
            application.PreRequestHandlerExecute+=new EventHandler(application_PreRequestHandlerExecute);
            application.PreSendRequestContent
+=new EventHandler(application_PreSendRequestContent);
            application.AcquireRequestState
+=new EventHandler(application_AcquireRequestState);
            application.AuthenticateRequest
+=new EventHandler(application_AuthenticateRequest);
            application.AuthorizeRequest
+=new EventHandler(application_AuthorizeRequest);
            application.PostRequestHandlerExecute
+=new EventHandler(application_PostRequestHandlerExecute);
            application.PreSendRequestHeaders
+=new EventHandler(application_PreSendRequestHeaders);
            application.ReleaseRequestState
+=new EventHandler(application_ReleaseRequestState);
            application.ResolveRequestCache
+=new EventHandler(application_ResolveRequestCache);
            application.UpdateRequestCache
+=new EventHandler(application_UpdateRequestCache);
        }

在这里我把HttpApplication的全部事件都声明了一遍,然后在每个方法的Response.Write写到浏览器一些内容,下面便是执行顺序:
This is BeginRequest
This is AuthenticateRequest
This is AuthorizeRequest
This is ResolveRequestCache
This is AcquireRequestState
This is PreRequestHandlerExecute

Hello World!!

This is PostRequestHandlerExecute
This is ReleaseRequestState
This is UpdateRequestCache
This is EndRequest
This is PreSendRequestHeaders
This is PreSendRequestContent
大家可以看到,BeginRequest是第一个,中间的Hello World是发生HttpHandler的地方,大家自己写的时候可以单步跟踪一下就会明白了。说了这么多还没提到Url重写,我们的Url重写处理方法就放在这个过程中,因为这是http请求的必经之路。http://www.microsoft.com/china/msdn/library/webservices/asp.net/URLRewriting.mspx?mfr=true这里有篇介绍Url重写的文章写的比较经典,还有源码下载。作者提供了两个方法重写Url,两个方法即两个不同的地方,一个在HttpModule中一个在HttpHandler中,不过并不是实现的IHttpHandler接口而是IHttpHandlerFactory接口,具体处理方法可下载源码并进行单步跟踪。如果看明白了,那么恭喜你,跟我一样,对Url重写入门了,呵呵。。。。。
ps:建议现看看《ASP.NET Framework深度历险》这个55页电子书,这样在看Scott Mitchell的那篇Url的文章会好懂些