Why does Result return the correct status code only in the response body?

Why does Result return the correct status code only in the response body?

Problem Description:

Whenever I return any Result it is always 200 status code. For example check this code

public async Task<IResult> Login(UserLoginDto login)
    var user = await _userMgr.FindByEmailAsync(login.Email);
    if (user != null)
        var result = await _signinManager.PasswordSignInAsync(user, login.Password, false, false);

        if (result.Succeeded)
            var token = await _authRepo.GenerateToken(user);
            _logger.LogInformation("User with email: {Email} is logged in ", user.Email);
            return Results.Ok(new { user.Email,  Token = token });
            return Results.Unauthorized();
    return Results.NotFound();

always returns the correct status code inside the response body only and 200Ok in the response header. How do i fix this weird behavior? i have looked into the .net 6 documentation but to no avail.


this is the middlewere

var app = builder.Build();
await app.SystemInit(builder.Configuration);


// Configure the HTTP request pipeline.
//todo: disable cors for production env
if (app.Environment.IsDevelopment())



// app.UseHttpsRedirection();



Solution – 1

The reason why all the result method will return 200 inside the MVC action is it is used for minimal api not for MVC action method.

Th result.notfound method will build a notfound.cs object and this notfound.cs object contains a executeasync method which is used to build the httpresponse.

Since you just return the Iresult instead of using the ExecuteAsync mehtod, so it will just return the json object to the client instead of calling the ExecuteAsync method to write a new response.

The right way to return the status codes inside the MVC action method you should use IActionResult’s NotFound() method.

If you are using minimal API, you could use iresult like this:

app.MapGet("/weatherforecast", () => Results.NotFound());
Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.