dcsimg
Results 1 to 1 of 1

Thread: Converting HttpResponseMessage to IActionResult

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2013
    Posts
    60

    Converting HttpResponseMessage to IActionResult

    So I'm having a few issues with a Web API in .NET Core. The project was originally in standard .NET and I'm converting it over. However, I'm having a few issues with the API that returns a PDF to an angular app inside MVC.

    Long story short, it's not returning the content in the header as it did in the old project with HttpResponseMessage and I'm not getting the PDF back to angular.
    I've tested the API to make sure it recieves the data and therefore I'm definitely getting the PDF.

    I have a feeling it's something to do with the 'HttpResponseMessage' and that changing it to IActionResult will fix it (he says, clutching at straws). I can't use IHttpActionResult as I'm interting from a custom controller and therefore can't use ApiController.

    I'm not 100% on how to go about doing this with IAction result, so if anyone could point me in the rigth direction on how to go about doing this- that would be massively appriciated. Luckily, it's not a massive amount of code. Thanks in advance!

    Code:
        public class TestController : BaseController
        {
            public TestController(ICacheHelper cacheHelper) : 
                base(cacheHelper)
            {
            }
    
            [Route("api/fingerscrossed")]
            [HttpPost]
            public HttpResponseMessage GetDocument([FromBody] UrlAndTokenModel data)
            {
               
                var client = new HttpClient();
    
                client.DefaultRequestHeaders.Add("Authorization", data.Token);
    
                var responseTask = client.GetAsync(data.UrlEndpoint);
    
                responseTask.Wait();
    
                var result = responseTask.Result;
                
                byte[] finalResult = null;
    
                if (result.IsSuccessStatusCode)
                {
    
                    var readTask = result.Content.ReadAsByteArrayAsync();
                    readTask.Wait();
    
                    finalResult = readTask.Result;
    
                }
    
                var httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK);
                
                httpResponseMessage.Content = new ByteArrayContent(finalResult);
    
                httpResponseMessage.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
    
                httpResponseMessage.Content.Headers.ContentDisposition.FileName = "mytestpdf.pdf";
    
                //httpResponseMessage.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
    
                httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
    
                return httpResponseMessage;
            }
    
        }
    }
    Last edited by J95W; Apr 25th, 2019 at 06:08 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width