IHttpHandler的学习(1)

IHttpHandler的那些事

今晚看了一晚上的IHttpHAndler的知识,

在自定义了Httphandler后,在配置webconfig里配置也是个技术活,什么集成模式,什么asp管道什么的;

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <!--
 3   有关如何配置 ASP.NET 应用程序的详细信息,请访问
 4   http://go.microsoft.com/fwlink/?LinkId=169433
 5   -->
 6 <configuration>
 7   <system.web>
 8     <compilation debug="true" targetFramework="4.5" />
 9     <httpRuntime targetFramework="4.5" />
10 
11     <!--下面的这一种光错-->
12     <!--<httpHandlers>
13       <add  name="MyFirstHandler" verb="*" path="*" type="IHttpHandlerDemo.MyFirstHandler, IHttpHandlerDemo"/>
14     </httpHandlers>-->
15     
16     
17   </system.web>
18   <!--下面的这一种就ojbk了-->
19   <system.webServer>
20       <!--<modules>
21         <add name="MyFirstHandler" type="IHttpHandlerDemo.MyFirstHandler" />
22       </modules>-->
23       <handlers>
24         <add name="MyFirstHandler" path="*" verb="*" type="IHttpHandlerDemo.MyFirstHandler" preCondition="integratedMode" />
25       </handlers>
26     </system.webServer>
27 </configuration>
View Code
 1 public partial class TestPage : System.Web.UI.Page
 2     {
 3 //Page类也是继承至IHttpHandler,所以也就是先执行的Httphandler的注册事件
 4         protected void Page_Load(object sender, EventArgs e)
 5         {
 6 
 7         }
 8 //然后你会发现这个时候请求会进到ProcessRequest()方法,而不会进到Page_Load()里面了,至于原因,这和Page类里面的封装有关系。要说明的是所有实现了IHttpHandler接口的类型都可以在ProcessRequest()方法里面处理当前http请求;即使你在PageLoad里写东西,也不会在里面执行了;
 9 因为重写了ProcessRequest这个方法
10         public void ProcessRequest(HttpContext context)
11         {
12             context.Response.Write("你好");
13         }
14     }
Page_Load和ProcessRequest

而且,在自定义了Httphandler后,程序会先运行Httphandler里的程序;所以这样就会可以先加载二维码或者是否登录的情况(看了好多的博文,这2提到的最多)

ASP.NET:使用HttpModule(给页面添加页头和页尾,重写URL)

一点一点学ASP.NET之基础概念——HttpHandler

IHttpModule实现URL重写

MVC框架的原理详解

 1 public class TestMyHandler:IHttpHandler
 2     {
 3         public bool IsReusable
 4         {
 5             get { return false; }
 6         }
 7 
 8         public void ProcessRequest(HttpContext context)
 9         {
10             context.Response.Write("从asex页面进来");
11 
12             //throw new NotImplementedException();
13         }
14     }
IHttpHandler包括的方法
 1 namespace MyTestMVC
 2 {
 3     public class TestMyModule:IHttpModule
 4     {
 5         public void Dispose()
 6         {
 7             //throw new NotImplementedException();
 8         }
 9 
10         public void Init(HttpApplication app)
11         {
12             //事件注册
13             app.BeginRequest += app_BeginRequest;
14             app.EndRequest += app_EndRequest;
15         }
16 
17         void app_EndRequest(object sender, EventArgs e)
18         {
19             var app = (HttpApplication)sender;
20             app.Context.Response.Write("请求结束");
21         }
22 
23         void app_BeginRequest(object sender, EventArgs e)
24         {
25             var app = (HttpApplication)sender;
26             app.Context.Response.Write("请求开始");
27         }
28     }
29 }
IHttpModule包含的方法

在Init方法里面,通过HttpApplication对象来注册请求的事件。这样,每次发起一次http请求的时候都进到这两个方法。

当然,这些注册就能执行了吗?想得美,系统哪里知道你这个自定义HttpModule的存在,所以必须要在Web.config里面声明一下。

仅仅是注册了是不能够行的呀,还必须得留有“备案”才能行呀;不能你买个域名就让你发布网站呀,得备案;

在webconfig里备案,我称它为程序中的备案局。

<system.webServer>
    <modules>
        <add name="TestMyModule" type="MyTestMVC.TestMyModule, MyTestMVC" preCondition="integratedMode" />
    </modules>
</system.webServer>

  

原文地址:https://www.cnblogs.com/ZkbFighting/p/8975427.html