个人IHttpHandler,IHttpModule认识
IHttpHandler
自定義一般處理程序類及配置:
這里只是簡單講講"可以這么做",對于原理性底層的東西,博客園有詳細(xì)資料,這里略過.(IIs處理機(jī)制,文件處理映射等)
對于一般的情況下,.Net本身已經(jīng)提供了.ashx處理程序,就是實(shí)現(xiàn)IHttpHandler接口,只要在PR方法寫我們自己的邏輯代碼即可;
本質(zhì)也是IIS收到后綴名為.ashx文件,交給isapi.dll映射給相應(yīng)的處理程序進(jìn)行處理,這樣我們也可以自定義程序來處理自定義后綴名文件;
??? /// <summary>
??? /// 根據(jù)業(yè)務(wù)返回?cái)?shù)據(jù)
??? /// </summary>
??? public class MyHttpHandler:IHttpHandler
??? {
??????? public bool IsReusable
??????? {
??????????? get { return true; }
??????? }
??????? public void ProcessRequest(HttpContext context)
??????? {
??????????? context.Response.Write("處理自定義后綴名程序文件");
??????? }
??? }
假設(shè)自定義一個后綴為.YZR的文件,使用MyHttpHandler來處理,需要在webconfig文件進(jìn)行配置一下:
<system.webServer>
??? <handlers>
????? <add name="myhandler" path="*.YZR" verb="GET,POST" type="UI.MyHttpHandler"/>
??? </handlers>
</system.webServer>
path指后綴名,verb指提交方式,type指處理程序類的全名稱;
Note:
lIIS集成模式下配置 l <system.webServer> l<!--適配IIS集成模式--> l??? <handlers> l????? <add name="iishander" path="*.do" verb="*" type="WebApplication1.IISHandler1"/> l??? </handlers> l? </system.webServer> lIIS經(jīng)典模式下配置 l<system.web> l <httpHandlers> l????? <add path="*.do" verb=“GET,POST" type="WebApplication1.IISHandler1" /> l??? </httpHandlers> l? </system.web> lVerb:注意一定要寫成大寫的GET,POST IHttpHandler可以做驗(yàn)證碼,通過Ajax方式的交互等 IHttpModule提供給程序員自定義功能后,注冊到事件管道上運(yùn)行;
在Asp.Net或者Asp.net MVC中一個http的請求相應(yīng)都會跑管道事件,IHttpModule就是在這個地方進(jìn)行一些過濾處理的,當(dāng)然.Net Framework已經(jīng)對管道事件進(jìn)行了處理(從BeginRequest開始,對用戶信息權(quán)限進(jìn)行檢驗(yàn),在第7個管道完成了緩存的處理,8管道.ashxPR方法,9-10Session的處理,11,12處理aspx頁面等我們寫的代碼,13,14開始釋放請求,15,16完成處理完的緩存,17,18日志,19渲染);我們自己也可以在這個基礎(chǔ)進(jìn)行添加自己的代碼:
public class MyHttpModule:IHttpModule
??? {
??????? public void Dispose()
??????? {
??????????? throw new NotImplementedException();
??????? }
??????? public void Init(HttpApplication context)
??????? {
??????????? context.BeginRequest += context_BeginRequest;
??????? }
??????? void context_BeginRequest(object sender, EventArgs e)
??????? {
??????????? HttpContext.Current.Response.Write("已經(jīng)經(jīng)過我手處理完了");
??????? }
??? }
?<modules>
????? <add name="mymodule" type="UI.MyHttpModule"/>
??? </modules>
? </system.webServer>
這里每當(dāng)http請求過來,都會走自定義的MyHttpModule.
實(shí)際開發(fā)的話,都是在最開始就截取http請求,即在BeginRequest,這樣在全局文件中注冊管道事件即可,并不需要自己自定義:
一般的話,IHttpModule可以做url的重寫(偽靜態(tài)),圖片的防盜鏈(或者圖片的水印處理)等等
Url的重寫:
protected void Application_BeginRequest(object sender, EventArgs e)
??????? {
??????????? string oldUrl = Context.Request.RawUrl;
??????????? string newUrl = string.Empty;
??????????? System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex("/Index/(.*)/(.*)");
??????????? if(reg.IsMatch(oldUrl))
??????????? {
??????????????? newUrl = reg.Replace(oldUrl, "/Index.aspx?id=$1&username=$2");
??????????????? Context.RewritePath(newUrl);
??????????? }
??????? }
具體的寫法可以根據(jù)邏輯進(jìn)行改造;
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/Francis-YZR/p/4770919.html
總結(jié)
以上是生活随笔為你收集整理的个人IHttpHandler,IHttpModule认识的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。