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
  • Do you have [Produces("application/json")] at the top of your controller? Commented Jan 18, 2018 at 10:02
  • @SBFrancies nope only [HttpGet] [Route("Download/{documentId}")] Commented Jan 18, 2018 at 10:03
  • I mean on the controller itself, not the action. Commented Jan 18, 2018 at 10:08
  • @SBFrancies nope only a route. Commented Jan 18, 2018 at 10:09
  • 1
    The answers to this question might help you: stackoverflow.com/questions/42460198/… Commented Jan 18, 2018 at 11:33

3 Answers 3

8

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

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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 Comment

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()....
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 Comments

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?
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.
And the questioner said that ResponseType ... would not work in .net core. The reference to ProducesResponseType is at least part of the answer.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.