【问题标题】:FileContentResult write to HttpResponseMessage.ContentFileContentResult 写入 HttpResponseMessage.Content
【发布时间】:2017-10-15 08:09:05
【问题描述】:

我无法在 .net core 1.1 中触发以下代码。它正在从服务器导出 SSRS 报告,并返回 FileResult 类型。我正在转换为 FileContentResult,然后转换为 memorystream,然后转换为 bytearray。该文件已生成,但 response.content 仅具有包含正确设置的内容长度标头的标头,但没有 bytearray 内容本身。这总是空的。正在使用 axios 从 Vue.JS 应用程序将内容检索到 .net 核心。

    [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;
    }

我认为问题出在它设置 response.content 或可能是 ms.toarray() 调用的区域,因为它确实会导致 readcount 和 writecount 异常警告,但我也读过这是故意的微软出于某种原因。

【问题讨论】:

  • 你能把代码放在它的实际方法和控制器中吗?这样就可以更好地理解了。
  • 感谢您的评论。其实我今晚刚刚解决了这个问题。我会在一秒钟内发布答案。不过你是对的,我会添加额外的代码。

标签: c# reporting-services asp.net-core memorystream


【解决方案1】:

经过更多调查,问题与它是一个 .net 核心项目有关。 HttpResponseMessage 作为旧 Web api 规范的一部分在 .net 核心中已被弃用,尽管它仍然可以被称为引用 System.Net.Http 命名空间。响应将运行,但无法正确序列化字节数组,并且不会存储 response.content,仅存储标头。

有一个涉及为某些 Web api 功能添加兼容性的修复程序。

如果您在通过 HttpResponseMessage 请求 memorystream / bytearrays 时在 .net 核心 Web 应用程序中收到奇怪的 xml 响应而不是实际正文内容,则需要安装 microsoft.aspnetcore.mvc.webapicompatshim 参考,然后添加 services.mvc.addwebapiconventions () 到您的 startup.cs 文件的 configureservices 部分。其他一切都可以保持不变。

see here 了解更多信息。

【讨论】:

    猜你喜欢
    • 2014-09-17
    • 2018-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-08
    • 2015-03-05
    • 1970-01-01
    相关资源
    最近更新 更多