Morgan
Morgan

Reputation: 369

FileContentResult write to HttpResponseMessage.Content

I cannot get the following code to fire in .net core 1.1. It is exporting an SSRS report from a server, and returning a FileResult type. I'm converting to FileContentResult and then to a memorystream then to bytearray. The file is generated, but the response.content only ever has the headers including the content length header which is correctly set, but not the bytearray content itself. This is always empty. The content is being retrieved using axios to .net core from a Vue.JS app.

    [HttpPost, Route("GetDailyInstitution")]
    public HttpResponseMessage GetDailyInstitution([FromBody] 
    ReportViewModels.DailyReportInst mydata)
    {

    HttpResponseMessage response = new HttpResponseMessage();     
    var model = this.GetReportViewerModel(Request);
    var mdate = mydata.mydate;
    model.ReportPath = "/xxxxx/xxxx/xxxx";
    model.AddParameter("InstitutionID", 
    mydata.InstitutionID.ToString());
    model.AddParameter("DayDate", mdate.ToString("dd/MM/yyyy"));
    model.ViewMode = AlanJuden.MvcReportViewer.ReportViewModes.Export;
    byte[] bytes;
    MemoryStream mstream = new MemoryStream();
    FileResult myfile = null;       
    FileContentResult myfilecontent = null;
    switch (mydata.mytype)
    {
         case "pdfMe":
         myfile = ExportReport("xxxxxx/xxxxxxxx/xxxx", "PDF");
         myfilecontent = (FileContentResult)myfile;
         bytes = myfilecontent.FileContents;
         mstream.Write(bytes, 0, bytes.Length);
         response.Content = new ByteArrayContent(mstream.ToArray());
         response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/pdf");
         break;
    }

    return response;
    }

I'm thinking the problem is in the area where it sets the response.content or possibly the ms.toarray() call as it does cause a readcount and writecount exception warning, but I've also read that's on purpose by Microsoft for some reason.

Upvotes: 0

Views: 1543

Answers (1)

Morgan
Morgan

Reputation: 369

After more investigation the problem was linked with the fact that it is a .net core project. The HttpResponseMessage has been deprecated in .net core as part of the older web api spec, even though it can still be called referencing the System.Net.Http namespace. The response will run, but it will fail to serialize the bytearray correctly and will not store the response.content, only the headers.

There is a fix that involves adding compatibility for some web api functionality.

If you are getting odd xml responses instead of actual body content in a .net core web app when requesting memorystream / bytearrays via HttpResponseMessage, you need to install the microsoft.aspnetcore.mvc.webapicompatshim reference then add services.mvc.addwebapiconventions() to the configureservices section of your startup.cs file. All else can remain the same.

see here for more info.

Upvotes: 1

Related Questions