Home » excel » c# – Excel File stops returning correctly from HttpResponseMessage after adding custom library reference

c# – Excel File stops returning correctly from HttpResponseMessage after adding custom library reference

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am working on an ASP.NET Web Application(.NET Framework). I wrote the program below to return an excel file as the response which works.

I have also defined a custom common class library(for me and others to use) that can be added as a reference to this project in the same solution. However as soon as I add it as a reference to the project, when I call my http endpoint inside my controller, it stops returning the excel file and instead returns this JSON in the browser:

{  
   "Version":{  
      "_Major":1,
      "_Minor":1,
      "_Build":-1,
      "_Revision":-1
   },
   "Content":{  
      "Headers":[  
         {  
            "Key":"Content-Disposition",
            "Value":[  
               "attachment; filename=ExcelFileName.xlsx"
            ]
         },
         {  
            "Key":"Content-Type",
            "Value":[  
               "application/xlsx"
            ]
         }
      ]
   },
   "StatusCode":200,
   "ReasonPhrase":"OK",
   "Headers":[  
      {  
         "Key":"Pragma",
         "Value":[  
            "no-cache"
         ]
      },
      {  
         "Key":"Cache-Control",
         "Value":[  
            "no-cache"
         ]
      },
      {  
         "Key":"Date",
         "Value":[  
            "Wed, 14 Mar 2018 18:20:52 GMT"
         ]
      },
      {  
         "Key":"Set-Cookie",
         "Value":[  
            "ARRAffinity=123456;Path=/;HttpOnly;Domain=helperapp.azurewebsites.net"
         ]
      },
      {  
         "Key":"Server",
         "Value":[  
            "ServerName/1.0"
         ]
      },
      {  
         "Key":"X-AspNet-Version",
         "Value":[  
            "4.0.30319"
         ]
      },
      {  
         "Key":"X-Powered-By",
         "Value":[  
            "ASP.NET"
         ]
      }
   ],
   "RequestMessage":{  
      "Version":{  
         "_Major":1,
         "_Minor":1,
         "_Build":-1,
         "_Revision":-1
      },
      "Content":null,
      "Method":{  
         "Method":"GET"
      },
      "RequestUri":"https://helperapp.azurewebsites.net/hlper/Get?id=123",
      "Headers":[  

      ],
      "Properties":{  

      }
   },
   "IsSuccessStatusCode":true
}

My code is setup like below which returns the excel file fine, but not when I add the custom reference to the project:

[System.Web.Http.HttpGet, Route("api/ReturnExcelFile")]
public async Task<HttpResponseMessage> ReturnExcelFile(Guid id)
{        
    HttpClient client = new HttpClient();
    HttpResponseMessage response = await client.GetAsync(request_uri);

    byte[] fileBytes = File.ReadAllBytes(excel_file_path);
    MemoryStream fileStream = new MemoryStream(fileBytes);
    response.Content = new StreamContent(fileStream);

    var responseHeader = response.Content.Headers;
    responseHeader.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
    responseHeader.ContentDisposition.FileName = ExcelFileName.xlsx;
    responseHeader.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/xlsx");

    return response;
}

I have seen many posts like this and this but their solutions do not work.

What is causing this and how can I resolve this issue?

How to&Answers:

Found out what was causing the issue. Very subtle to catch.

TLDR: Re-create your class libraries to be of type “Class Library (.NET Framework)”

The custom common libraries that were created were of type “Class Library (.NET Standard)”. For some reason creating a class library in this format then adding it as a reference causes the HTTP response to come back in a bad state.

I re-created the class libraries to be of type “Class Library (.NET Framework)” and it worked correctly and the response returned the excel file I created.