ProducesAttribute causes "No output formatter was found for content types"

Viewed 844

Consider this simple controller action:

[HttpGet("{imageId}")]
[ResponseCache(Duration = 604800)]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[Produces("image/jpeg")]
public async Task<IActionResult> GetImageAsync(int imageId)
{
    if (imageId <= 0)
    {
        return NotFound();
    }

    byte[] imageBytes = await _imageProvider.GetImageAsync(
        imageId,
        Request.HttpContext.RequestAborted).ConfigureAwait(false);

    if (imageBytes is null)
    {
        return NotFound();
    }

    return File(imageBytes, MediaTypeNames.Image.Jpeg);
}

This method works fine, however in telemetry I am getting this for every call:

Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor: Warning: No output formatter was found for content types 'image/jpeg, image/jpeg' to write the response.

Simply commenting out the ProducesAttribute prevents the telemetry from being logged.

I want to keep ProducesAttribute because it enables my Swagger UI page to work. In Swagger it shows this API has an expected output media type of image/jpeg. If I remove the attribute it changes to text/plain and it doesn't render correctly.

So the question is, how can I fix this controller action to not create a bunch of unnecessary telemetry, while also allowing Swagger UI to work correctly?

0 Answers
Related