Closed
Description
In a ASP.NET Core web API project, an exception is thrown when ReportApiVersions is true as the versioning framework tries to add a header to an HTTP response after the response started being streamed to the client.
Simple repro:
- In an ASP.NET core web API, have this in the Startup:
services.AddApiVersioning(o =>
{
o.ReportApiVersions = true;
o.AssumeDefaultVersionWhenUnspecified = true; // only needed to facilitate the test, not needed for repro if the api version is supplied.
});
- In a controller class, add a version, e.g.
[ApiVersion("1.0")]
[Route("api/[controller]")]
public class ValuesController : Controller
- Then in the method, write something to Reponse.Body, for example:
[HttpGet]
public async Task Get()
{
Response.StatusCode = 200;
using (var writer = new StreamWriter(Response.Body))
{
await writer.WriteAsync("Hello");
}
}
When the controller method is fired, an exception is thrown as the versioning framework tries to add a header to the response after the HTTP streaming has started, and thus the header had already been sent.