API response is missing but not middleware response updates

Harish Lakshmana 20 Reputation points
2025-01-24T12:00:55.5133333+00:00

I was experimenting asp.net core (net9.0) custom middleware with below sample code. In API response I get the text written by middleware but the API response itself is missing. Any insights to help me understand on what is actually happening ?

This is response I get for the API call

Middleware 1. Passing to the next middleware! Middleware 2. Passing to the next middleware! Terminal middleware 1! Middleware 2. Received from previous middleware. Middleware 1. Received from previous middleware.

WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
WebApplication app = builder.Build();

app.UseHsts();

// custom nonterminal middleware, only to be added by app.Use();
app.Use(async (context, next) =>
{
    await context.Response.WriteAsync("Middleware 1. Passing to the next middleware!\r\n");

    String? authHeader = context.Request.Headers.Authorization;
    if (String.IsNullOrWhiteSpace(authHeader) || !authHeader.ToLower().StartsWith("bearer"))
    {
        await context.Response.WriteAsync("Middleware 1. Terminating pipeline since auth header is not present.\r\n");
    }
    else
    {
        // Call the next middleware in the pipeline
        await next.Invoke();

        await context.Response.WriteAsync("Middleware 1.  Received from previous middleware.\r\n");
    }
});

app.Use(async (context, next) =>
{
    await context.Response.WriteAsync("Middleware 2. Passing to the next middleware!\r\n");

    // Call the next middleware in the pipeline
    await next.Invoke();

    await context.Response.WriteAsync("Middleware 2.  Received from previous middleware.\r\n");
});

// custom terminal middleware, only to be added by app.Run();
app.Run(async context =>
{
    await context.Response.WriteAsync("Terminal middleware 1!\r\n");
});

app.Run(async context =>
{
    // never reached since terminal Middleware 1 will not forward to this middleware
    await context.Response.WriteAsync("Hello from terminal middleware 2!\r\n");
});

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

app.Run();


.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,103 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,806 questions
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 29,946 Reputation points
    2025-01-24T13:22:08.43+00:00

    I was experimenting asp.net core (net9.0) custom middleware with below sample code. In API response I get the text written by middleware but the API response itself is missing. Any insights to help me understand on what is actually happening ?

    If I understand correctly, you expect a response from a controller.

    The run delegate terminates the pipeline as described in your code comments. The controller is never reached because that's how your code is designed.

    https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-9.0#run-delegates

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 73,101 Reputation points
    2025-01-24T16:48:19.62+00:00

    your middleware registration is list:

    1. middleware 1
    2. middleware 2
    3. run 1
    4. run 2
    5. map

    as .Run() does not call Next() to run the next middleware from the list, your run 1 middleware its the end of the line. so after run 1 is called it returns to middleware 2, when when coupled returns to middleware 1

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.