ASP.NET What is the difference between core middleware and HttpModule



stay ASP.NET One of the biggest changes in the core is the change to the HTTP request pipeline ASP.NET We know that in the pastHttpHandlerandHttpModuleBut now these have been replaced withmiddleware Now let’s take a look at their differences.


Handlers handle specific requests based on extensions, and httphandlers run as an extension server, while providing ASP.NET Respond to the request. He is a realizationSystem.Web.IHttphandlerInterface. Any class that implements the ihttphandler interface can be used as the target program of HTTP request processing response.
It provides file specific extensions to handle incoming requests,
ASP.NET The framework provides some default HTTP handlers, the most common of which is HTTP handler.aspxDocuments. Some default handlers are provided below.

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

Create a custom HttpHandler

public class CustomHttpHandler:IHttpHandler
    public bool IsReusable
        //Specifies whether the handler can be reused
        get {return true;}
    public void ProcessRequest(HttpContext context)
        throw new NotImplementedException();

stay web.config Add configuration item to

Asynchronous httphandlers

Asynchronous, you need to inheritHttpTaskAsyncHandlerClass,HttpTaskAsyncHandlerClassIHttpTaskAsyncHandlerandIHttpHandlerInterface

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

        throw new NotImplementedException();


Here’s from 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.

HttpModuleSimilar to a filter, it is an event based access to events throughout the application’s lifecycle from initiation to completion

Customize an 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 ("before request processing");

        void EndRequest(object sender, EventArgs e)
            ((HttpApplication)sender). Context.Response.Write (after the end of request processing));

web.config Medium configuration


Middleware can be regarded as a small application component integrated into HTTP request pipeline ASP.NET The combination of HttpModule and HttpHandler can handle authentication, log request recording and so on.

Similarities between middleware and HttpModule

Both middleware and httpmoodle can handle each request and can be configured to return our own definition.

The difference between middleware and HttpModule

HttpModule middleware
adopt web.config or global.asax to configure Add middleware to startup file
The order of execution cannot be controlled because the order of modules is primarily based on application lifecycle events It can control the execution content and execution order, and execute according to the order of adding.
The order of request and response execution remains unchanged The order of response middleware is opposite to that of request
Httpmodules can attach code for specific application events Middleware is independent of these events

Middleware example

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
      if (env.IsDevelopment())




      app.UseEndpoints(endpoints =>

In the above code fragment, we have added some middleware, and we also have the order of middleware.


How ASP.NET Core 1.0 Middleware is different from HttpModule

Recommended Today

MySQL partition

MySQL partition Advantages of partitioning Store more data To optimize the query, only scan one or more necessary partitions. For count () and sum (), only summarize the partition statistics For data that is out of date or does not need to be saved, partitioning is faster Data queries are distributed across multiple disks to […]