Using inline middleware code as anonymous methods - Use, Run, Map, and MapWhen
In this recipe, we will learn how to use inline middleware code in the Configure()
method of Startup.cs
.
Getting ready
We create an empty ASP.NET Core web application with Visual Studio, using .NET Core or .NET Framework.
How to do it...
We'll create a new project and use different types of middleware:
- 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...