ASP NET Core Minimal API always returns 200 instead of specified 204
Asked Answered
M

1

9

Even when I say return 204 (NoContent), it returns 200. Why?

app.MapGet("/api/alive", (context) => Task.FromResult(new StatusCodeResult(204)));

Status code in browser is 200

Marmara answered 17/9, 2021 at 7:31 Comment(6)
Can you show more of your code please so we can see how youre getting to this response. Typically I would expect to see Return NoContent();Makeup
It makes no difference if you Use Results.NoContent(), new NoContentResult() or new StatusCodeResult(204)Marmara
Did you check the documentation? Are you aware of the signature of second argument(RequestDelegate) ? Where did you get that it has a return type?Colner
Changing the type to RequestDelegate did the trickMarmara
Do not edit the answer into your question. I have rolled back your question to the original version.Ingres
ok, didn't know it would be a problem.Marmara
M
11

The solution is to change the signature a bit:

app.MapGet("/api/alive", () => Results.NoContent());

(context) alone does not work, Edit: as well as (RequestDelegate context) does not work.

app.MapGet("/api/alive", (context) => Results.NoContent());
app.MapGet("/api/alive", (RequestDelegate context) => Results.NoContent());
Marmara answered 17/9, 2021 at 7:51 Comment(2)
Really frustrated with this. Is it a bug? Is there a work around? Trying to map to a handler function so I can use attributes, etc, and ASP.NET 6 always returns an empty 200 even when I return an IResult from the handler.Camboose
You can make it even simpler. app.MapGet("/api/alive", Results.NoContent);.Propend

© 2022 - 2024 — McMap. All rights reserved.