日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#强化系列:HttpModule,HttpHandler,HttpHandlerFactory简单使用

發布時間:2025/7/14 C# 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#强化系列:HttpModule,HttpHandler,HttpHandlerFactory简单使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這三個對象我們在開發Asp.net程序時經常會用到,似乎很熟悉,但有時候又不太確定。本文通過一個簡單的例子來直觀的比較一下這三個對象的使用。
HttpModule:Http模塊,可以在頁面處理前后、應用程序初始化、出錯等時候加入自己的事件處理程序
HttpHandler:Http處理程序,處理頁面請求
HttpHandlerFactory:用來創建Http處理程序,創建的同時可以附加自己的事件處理程序

例子很簡單,就是在每個頁面的頭部加入一個版權聲明。
一、HttpModule
這個對象我們經常用來進行統一的權限判斷、日志等處理。
例子代碼:

????public?class?MyModule?:?IHttpModule
????{
????????public?void?Init(HttpApplication?application)
????????{
????????????application.BeginRequest?+=?new?EventHandler(application_BeginRequest);
????????}

????????void?application_BeginRequest(object?sender,?EventArgs?e)
????????{
????????????((HttpApplication)sender).Response.Write("Copyright?@Gspring<br/>");
????????}

????????public?void?Dispose()
????????{
????????}
????}

web.config中配置:

??????<httpModules>
????????<add?name="test"?type="HttpHandle.MyModule,?HttpHandle"/>
??????</httpModules>

在Init方法中可以注冊很多application的事件,我們的例子就是在開始請求的時候加入自己的代碼,將版權聲明加到頁面的頭部?

二、HttpHandler

這個對象經常用來加入特殊的后綴所對應的處理程序,比如可以限制.doc的文件只能給某個權限的人訪問。
Asp.Net中的Page類就是一個IHttpHandler的實現
例子代碼:

????public?class?MyHandler?:?IHttpHandler
????{
????????public?void?ProcessRequest(HttpContext?ctx)
????????{
????????????ctx.Response.Write("Copyright?@Gspring<br/>");
????????}
????????public?bool?IsReusable
????????{
????????????get?{?return?true;?}
????????}
????}

web.config中配置:

??????<httpHandlers>
??????<add?verb="*"?path="*.aspx"?type="HttpHandle.MyHandler,?HttpHandle"/>
??????</httpHandlers>

這個對象主要就是ProcessRequest方法,在這個方法中輸出版權信息,但同時也有一個問題:原來的頁面不會被處理,也就是說頁面中只有版權聲明了。那么所有的aspx頁面都不能正常運行了

三、HttpHandlerFactory

這個對象也可以用來加入特殊的后綴所對應的處理程序,它的功能比HttpHandler要更加強大,在系統的web.config中就是通過注冊HttpHandlerFactory來實現aspx頁面的訪問的:

??????<add?path="*.aspx"?verb="*"?type="System.Web.UI.PageHandlerFactory"?validate="true"/>

HttpHandlerFactory是HttpHandler的工廠,通過它來生成不同的HttpHandler對象。

例子代碼:

????public?class?MyHandlerFactory?:?IHttpHandlerFactory
????{
????????public?IHttpHandler?GetHandler(HttpContext?context,?string?requestType,?string?url,?string?pathTranslated)
????????{
????????????PageHandlerFactory?factory?=?(PageHandlerFactory)Activator.CreateInstance(typeof(PageHandlerFactory),?true);
????????????IHttpHandler?handler?=?factory.GetHandler(context,?requestType,?url,?pathTranslated);

????????????//執行一些其它操作
????????????Execute(handler);

????????????return?handler;
????????}

????????private?void?Execute(IHttpHandler?handler)
????????{
????????????if?(handler?is?Page)
????????????{
????????????????//可以直接對Page對象進行操作
????????????????((Page)handler).PreLoad?+=?new?EventHandler(MyHandlerFactory_PreLoad);
????????????}
????????}

????????void?MyHandlerFactory_PreLoad(object?sender,?EventArgs?e)
????????{
????????????((Page)sender).Response.Write("Copyright?@Gspring<br/>");
????????}

????????public?void?ReleaseHandler(IHttpHandler?handler)
????????{
????????}
????}

web.config中配置:

??????<httpHandlers>
??????<add?verb="*"?path="*.aspx"?type="HttpHandle.MyHandlerFactory,?HttpHandle"/>
??????</httpHandlers>

在例子中我們通過調用系統默認的PageHandlerFactory類進行常規處理,然后在處理過程中加入自己的代碼,可以在Page對象上附加自己的事件處理程序。

附一個小的惡作劇:
我們可以開發好aspx頁面,然后把web應用程序發布后把所有的aspx文件的后綴都改為spring,再在web.config中加入配置:

??????<httpHandlers>
??????<add?verb="*"?path="*.spring"?type="HttpHandle.MyHandlerFactory,?HttpHandle"/>
??????</httpHandlers>
??????<compilation>
????????<buildProviders>
??????????<add?extension=".spring"?type="System.Web.Compilation.PageBuildProvider"/>
????????</buildProviders>
??????</compilation>

buildProviders是用來指定spring后綴的編譯程序,我們把它設置成和aspx一致就可以了。如果在IIS中發布的話還需要在應用程序配置中加入spring的后綴映射。
然后我們就可以通過?http://../.../*.spring來訪問我們的網站了

出處:https://www.cnblogs.com/firstyi/archive/2008/05/07/1187274.html

轉載于:https://www.cnblogs.com/hedianzhan/p/8410396.html

總結

以上是生活随笔為你收集整理的C#强化系列:HttpModule,HttpHandler,HttpHandlerFactory简单使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。