Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

228
Visualizações
MapGet execution sequence in pipeline

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?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Quoting from Routing in ASP.NET Core, Routing basics:

Apps typically don't need to call UseRouting or UseEndpoints. WebApplicationBuilder configures a middleware pipeline that wraps middleware added in Program.cs with UseRouting and UseEndpoints. However, apps can change the order in which UseRouting and UseEndpoints run by calling these methods explicitly. For example, the following code makes an explicit call to UseRouting:

app.Use(async (context, next) =>
{
    // ...
    await next(context);
});

app.UseRouting();

app.MapGet("/", () => "Hello World!");

[...]

If the preceding example didn't include a call to UseRouting, the custom middleware would run after the route matching middleware.

So the answer is because WebApplication.CreateBuilder(args) by default will order the request pipeline such that endpoints always run last, unless you explicitly say you want to change that order.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda