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?