How to do it...

We'll create a new project and use different types of middleware:

  1. First, let's use app.Use:
public void Configure(IApplicationBuilder app) 
{ 
  ... 
  app.Use(async (context, next) => 
  { 
    await context.Response.WriteAsync 
    ("First Inline middleware before Handlen"); 
    await next.Invoke(); 
    await context.Response.WriteAsync 
    ("First Inline middleware after Handle"); 
  }); 
  ... 
} 

next.Invoke() will call the next middleware in the pipeline.

We can add as many app.Use methods as we want. They will be executed in the order in which they were added on the pipeline.

To make an analogy with an HTTP module, the code executed before await next.Invoke() corresponds with the code in the BeginRequest method in a class implementing IHttpModule, and ...

Get ASP.NET Core MVC 2.0 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.