0

I just ran into a major headache working on a site that uses ASP.NET Core 6 minimal APIs.

public class GetUsersEndpoint : EndpointBase
{
    /// <inheritdoc />
    public override WebApplication MapEndpoints(WebApplication app)
    {
        app.MapGet("/api/users/", GetUsersAsync);
        return base.MapEndpoints(app);
    }

    /// <summary>
    /// Get a list of users
    /// </summary>
    [Authorize(Roles = AppRoles.SystemAdministrator),
     SwaggerOperation(Tags = new[] {"Users"})]
    private async Task<IResult> GetUsersAsync(HttpContext context)
    {
        await Task.Yield();
        return Results.StatusCode(400);
    }
}

No matter what I returned from the GetUsersAsync method above, ASP.NET Core 6 always returned an empty 200 response.

Finally, after about an hour of digging, I tried the following instead:

public class GetUsersEndpoint : EndpointBase
{
    /// <inheritdoc />
    public override WebApplication MapEndpoints(WebApplication app)
    {
        app.MapGet("/api/users/", GetUsersAsync);
        return base.MapEndpoints(app);
    }

    /// <summary>
    /// Get a list of users
    /// </summary>
    [Authorize(Roles = AppRoles.SystemAdministrator),
     SwaggerOperation(Tags = new[] {"Users"})]
    private async Task<IResult> GetUsersAsync(HttpResponse response)
    {
        await Task.Yield();
        return Results.StatusCode(400);
    }
}

Note the HttpContext parameter is now HttpResponse instead.

And it worked fine. It seems to me that if ASP.NET Core 6 minimal API binds to a handler that receives HttpContext directly, it appears to delegate all handling of the response to the method and does not subsequently execute the IResult or anything else.

However, I cannot seem to find any documentation describing this behavior. Am I on the right track, or did I miss something obvious?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Chris
  • 27,596
  • 25
  • 124
  • 225
  • Have you checked this [link](https://learn.microsoft.com/en-us/aspnet/core/tutorials/min-web-api?view=aspnetcore-6.0&tabs=visual-studio#differences-between-minimal-apis-and-apis-with-controllers) whitch shows the different bewteen controller api and minimal Api and also some features are not supported by minimal api? – Xinran Shen Apr 04 '22 at 09:54
  • That link says nothing about what I mentioned above. – Chris Apr 04 '22 at 20:53

0 Answers0