4

I'm quite new to the concept of a minimal API, but the documentation seems quite clear. You only have to use Results or TypedResults to return the response code you want.

So I made a very basic try:

app.MapPost("/api/test", async context =>
{
    var form = await context.Request.ReadFormAsync();
    var file = form.Files["File"];
    Results.NoContent();
});

But even with that, it always returns a 200 status code. I also checked a lot of other material from .NET 6 and 7 (I target .NET 7) with method delegates that have a return type of Task<IResult> as specified here in the documentation, but I got the same result (200). Note that I also tried returning NotFound, but I still got the same.

app.MapPost("/api/test", TestReturn);

static async Task<IResult> TestReturn(HttpContext context)
{
    var form = await context.Request.ReadFormAsync();
    var file = form.Files["File"];
    return TypedResults.NoContent();
}

Why can't I get anything else than a 200?

4
  • See following : learn.microsoft.com/en-us/aspnet/core/web-api/… Commented Aug 4, 2023 at 12:53
  • For some strange reason, the result status is always 200, but if you drop HttpContext context from the argument list, result status is just as you set it. Not sure why it's that, to me it's an error. Easily reproducible. Commented Aug 4, 2023 at 13:24
  • @WiktorZychla that is a known issue - check out my answer there are some links about that. Commented Aug 4, 2023 at 13:31
  • @GuruStron: indeed, your last link is a direct description. Glad someone already posted this, I was about to search and post if not found. Commented Aug 4, 2023 at 14:39

2 Answers 2

5

There are two issues here:

  1. You forgot to use return:

    app.MapPost("/api/test", async context =>
    {
        var form = await context.Request.ReadFormAsync();
        var file = form.Files["File"];
        return Results.NoContent();
    });
    
  2. There is a known issue with async handlers accepting HttpContext only, you can workaround by adding extra parameter (for example CancellationToken):

    app.MapPost("/api/test", async (HttpContext context, CancellationToken ct) =>
    {
        var form = await context.Request.ReadFormAsync();
        var file = form.Files["File"];
        return Results.NoContent();
    });
    

Also note that in ASP.NET Core 7 binding form files is supported out of the box:

app.MapPost("/api/test", async (IFormFileCollection files) =>
{
    var file = files["File"];
    return Results.NoContent();
});

See also:

Sign up to request clarification or add additional context in comments.

1 Comment

The CancellationToken was indeed the solution. I had tried the "return" before without success. And as for the IFormFile and IFormFile collection support, I knew but my real payload is multipart with string parameters and a file, which is not supported, but thanks for the tips.
0

You need to actually return the result using return. If you don't by default 200 Ok will be returned.

The following Program.cs will work and return 204 No Content on .NET 7. I've tested it using Postman. Make sure to get the URL, port as well as HTTP method right (e.g. GET http://localhost:5097/test).

FYI: the project was created using dotnet new webapi -o WebApiDemo and then just the Program.cs was changed to what is shown below:

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
var app = builder.Build();

app.MapGet("/test", () =>
{
    return TypedResults.NoContent();
});
app.MapControllers();
app.Run();

1 Comment

This still should not work for OP due to having async handler accepting HttpContext only

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.