ASP.NET Core中间件与HttpModule有何不同

前言

在ASP.NET Core中最大的更改之一是对Http请求管道的更改,在ASP.NET中我们了解HttpHandlerHttpModule但是到现在这些已经被替换为中间件那么下面我们来看一下他们的不同处。

HttpHandler

Handlers处理基于扩展的特定请求,HttpHandlers作为进行运行,同时做到对ASP.NET响应请求。他是一个实现System.Web.IHttphandler接口的类。任何实现IHttpHandler接口的类都可以作为Http请求处理响应的目标程序。
它提供了对文件特定的扩展名处理传入请求,
ASP.NET框架提供了一些默认的Http处理程序,最常见的处理程序是处理.aspx文件。下面提供了一些默认的处理程序。

HandlerExtensionDescription
Page Handler.aspxhandle normal WebPages
User Control Handler.ascxhandle Web user control pages
Web Service Handler.asmxhandle Web service pages
Trace Handlertrace.axdhandle trace functionality

创建一个自定义HttpHandler


public class CustomHttpHandler:IHttpHandler
{
    
    public bool IsReusable
    {
        //指定是否可以重用处理程序
        get {return true;}
    }
    
    public void ProcessRequest(HttpContext context)
    {
        //TODO
        throw new NotImplementedException();
    }
}

在web.config中添加配置项

<!--IIS6或者IIS7经典模式-->  
  <system.web>  
    <httpHandlers>  
      <add name="mycustomhandler" path="*.aspx" verb="*" type="CustomHttpHandler"/>  
    </httpHandlers>  
  </system.web>  
   
<!--IIS7集成模式-->  
  <system.webServer>  
    <handlers>  
       <add name="mycustomhandler" path="*.aspx" verb="*" type="CustomHttpHandler"/>  
    </handlers>  
  </system.webServer>  

异步HttpHandlers

异步的话需要继承HttpTaskAsyncHandler类,HttpTaskAsyncHandler类实现了IHttpTaskAsyncHandlerIHttpHandler接口

public class CustomHttpHandlerAsync:HttpTaskAsyncHandler
{
    
    public override Task ProcessRequestAsync(HttpContext context)
    {

        throw new NotImplementedException();
    }
}

HttpModule

下面是来自MSDN

Modules are called before and after the handler executes. Modules enable developers to intercept, participate in, or modify each individual request. Modules implement the IHttpModule interface, which is located in the System.Web namespace.

HttpModule类似过滤器,它是一个基于事件的,在应用程序发起到结束的整个生命周期中访问事件

自定义一个HttpModule

public class CustomModule : IHttpModule
    {
        public void Dispose()
        {
            throw new NotImplementedException();
        }

        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(BeginRequest);
            context.EndRequest += new EventHandler(EndRequest);
        }
        void BeginRequest(object sender, EventArgs e)
        {
            ((HttpApplication)sender).Context.Response.Write("请求处理前");
        }

        void EndRequest(object sender, EventArgs e)
        {
            ((HttpApplication)sender).Context.Response.Write("请求处理结束后");
        }
    }


web.config中配置

<!--IIS6或者IIS7经典模式-->  
<system.web>  
    <httpModules>  
      <add name="mycustommodule" type="CustomModule"/>  
    </httpModules>  
  </system.web>  
<!--IIS7集成模式-->  
<system.webServer>  
    <modules>  
      <add name="mycustommodule" type="CustomModule"/>  
    </modules>  
</system.webServer>  

中间件

中间件可以视为集成到Http请求管道中的小型应用程序组件,它是ASP.NET中HttpModule和HttpHandler的结合,它可以处理身份验证、日志请求记录等。

中间件和HttpModule的相似处

中间件和HttpMoudle都是可以处理每个请求,同时可以配置进行返回我们自己的定义。

中间件和httpModule之间的区别

HttpModule中间件
通过web.config或global.asax配置在Startup文件中添加中间件
执行顺序无法控制,因为模块顺序主要是基于应用程序生命周期事件可以控制执行内容和执行顺序按照添加顺序执行。
请求和响应执行顺序保持不变响应中间件顺序与请求顺序相反
HttpModules可以附件特定应用程序事件的代码中间件独立于这些事件

中间件示例

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }

在如上代码片段中我们有一些中间件的添加,同时也有中间件的顺序。

Reference

https://www.talkingdotnet.com/asp-net-core-middleware-is-different-from-httpmodule/

https://support.microsoft.com/en-us/help/307985/info-asp-net-http-modules-and-http-handlers-overview

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值