I try to create a simple WebAPI controller in .NET Framework 4.7.1 with one GET Endpoint which return a file (in these case a PDF), but when I call it, it return a JSON with the request info instead. I can't figure why, could you tell me where is my mistake please?
I tried returning different HttpContent such as StreamContent or ByteArrayContent, trying to replicate the response from How to return a file (FileContentResult) in ASP.NET WebAPI or https://www.aspsnippets.com/Articles/Return-Download-File-using-Web-API-in-ASPNet-MVC.aspx but it always seems to end with the same request info.
[HttpGet]
[Route("GetReport")]
public HttpResponseMessage GetReport()
{
FileStream stream = System.IO.File.Open(PDF_TEMP_PATH, FileMode.Open);
var result = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StreamContent(stream)
};
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "test.pdf"
};
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
return result;
}
Here is the result:
{"version":{"major":1,"minor":1,"build":-1,"revision":-1,"majorRevision":-1,"minorRevision":-1},"content":{"headers":[{"key":"Content-Disposition","value":["attachment; filename=test.pdf"]},{"key":"Content-Type","value":["application/pdf"]}]},"statusCode":200,"reasonPhrase":"OK","headers":[],"requestMessage":null,"isSuccessStatusCode":true}
But obviously I would like the PDF in the Stream instead.