前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ASP.NET Core中间件与HttpModule有何不同

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

作者头像
HueiFeng
发布2020-06-08 08:23:08
8520
发布2020-06-08 08:23:08
举报
文章被收录于专栏:HueiFeng技术专栏HueiFeng技术专栏

前言

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

HttpHandler

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

Handler

Extension

Description

Page Handler

.aspx

handle normal WebPages

User Control Handler

.ascx

handle Web user control pages

Web Service Handler

.asmx

handle Web service pages

Trace Handler

trace.axd

handle trace functionality

创建一个自定义HttpHandler

代码语言:javascript
复制
public class CustomHttpHandler:IHttpHandler
{

    public bool IsReusable
    {
        //指定是否可以重用处理程序
        get {return true;}
    }

    public void ProcessRequest(HttpContext context)
    {
        //TODO
        throw new NotImplementedException();
    }
}

在web.config中添加配置项

代码语言:javascript
复制
<!--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接口

代码语言:javascript
复制
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

代码语言:javascript
复制
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中配置

代码语言:javascript
复制
<!--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可以附件特定应用程序事件的代码

中间件独立于这些事件

中间件示例

代码语言:javascript
复制
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

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

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • HttpHandler
    • 创建一个自定义HttpHandler
      • 异步HttpHandlers
      • HttpModule
        • 自定义一个HttpModule
        • 中间件
        • 中间件和HttpModule的相似处
        • 中间件和httpModule之间的区别
        • 中间件示例
        相关产品与服务
        消息队列 TDMQ
        消息队列 TDMQ (Tencent Distributed Message Queue)是腾讯基于 Apache Pulsar 自研的一个云原生消息中间件系列,其中包含兼容Pulsar、RabbitMQ、RocketMQ 等协议的消息队列子产品,得益于其底层计算与存储分离的架构,TDMQ 具备良好的弹性伸缩以及故障恢复能力。
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档