I have 2 pieces of code
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/", () => "Hello World!");
app.Use(async (context, next) =>
{
if (context.Request.Headers["token"] != "my_token")
{
context.Response.StatusCode = 401;
return;
}
await next();
});
app.Run();
and
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.Use(async (context, next) =>
{
if (context.Request.Headers["token"] != "my_token")
{
context.Response.StatusCode = 401;
return;
}
await next();
});
app.MapGet("/", () => "Hello World!");
app.Run();
Difference is only location of app.MapGet()
For the both code app.Use()
is executed firstly, then app.MapGet()
.
What is a reason of this? Why isn't pipeline always executed based on the sequence of the middlewares?