5

I just moved my project to ASP.Net Core from ASP.Net 4.5. I've a REST API get that used to return a blob but now is returning JSON instead.

This is the old code:

[HttpGet]
[ResponseType(typeof(HttpResponseMessage))]
[Route("Download/{documentId}")]
public async Task<HttpResponseMessage> DownloadDocument(string documentId)
{
    try
    {
        var result = await TheDocumentService.DownloadDocument(documentId);

        return result;
    }
    catch (Exception ex)
    {
        return new HttpResponseMessage
        {
            StatusCode = HttpStatusCode.InternalServerError,
            Content = new StringContent(ex.Message)
        };
    }
}

The code in ASP.net Core is the same except for [ResponseType(typeof(HttpResponseMessage))] not working in ASP.Net Core, also the return result is also the same in both solutions.

But when looking at the response from the server in the client they differ.

enter image description here

So the only thing that differs them both from each other are the [ResponseType(typeof(HttpResponseMessage))]. Is there something equivalent in asp.net core?

6

3 Answers 3

5

Dot Net Core equivalent to [ResponseType(typeof(HttpResponseMessage))] is [Produces(typeof(HttpResponseMessage))]

1
  • Produces is useful for swagger docs, but the api response OP wants is either a file stream or 500 error, not a json serialisation of HttpResponseMessage. Nov 9, 2021 at 0:57
1

How to to return an image with Web API Get method

I solved it by changing my return:

[HttpGet]
[Route("Download/{documentId}")]
public async Task<IActionResult> DownloadDocument(string documentId)
{
    try
    {
        var result = await TheDocumentService.DownloadDocument(documentId);
        var content = await result.Content.ReadAsByteArrayAsync();
        return File(content, result.Content.Headers.ContentType.ToString());
    }
    catch (Exception ex)
    {
        return StatusCode(500, ex);
    }
}
1
  • 6
    Do not block on async methods by accessing Result. That's an excellent way to deadlock your application. Always use await, which you can do inline if that's your goal, i.e. return File(await result.Content.ReadAsByteArrayAsync().... Jan 18, 2018 at 14:35
0

Edit: @President Camacho said that [ResponseType...] would not work in .net core. It has been replaced by [ProducesResponseType]:

[HttpGet]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(List<int>))]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<ActionResult<IEnumerable<User>>> Get...()

The explanation from Microsoft is fine: https://learn.microsoft.com/en-us/aspnet/core/web-api/action-return-types?view=aspnetcore-6.0#iactionresult-type A quote from the website sums it up: "This attribute produces more descriptive response details for web API help pages generated by tools like Swagger."

And it's good for other team members who don't want to look at the source code to develop the UI.

3
  • 1
    Can you offer an explanation of what the [ProducesResponseType()] attribute does, and why you prefer this syntax over that offered in e.g., @President Camacho answer from a few years ago? Nov 9, 2021 at 1:12
  • The explanation from Microsoft is fine: learn.microsoft.com/en-us/aspnet/core/web-api/… - A quote from the website: "This attribute produces more descriptive response details for web API help pages generated by tools like Swagger." And it's good for other team members who don't want to look at the source code to develop the UI.
    – KarMau
    Nov 9, 2021 at 8:57
  • And the questioner said that ResponseType ... would not work in .net core. The reference to ProducesResponseType is at least part of the answer.
    – KarMau
    Nov 9, 2021 at 9:12

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.