我在ASP中创建了Wep API.Net内核返回PDF.这是我的代码:

public HttpResponseMessage Get(int id)
{
    var response = new HttpResponseMessage(System.Net.HttpStatusCode.OK);           
    var stream = new System.IO.FileStream(@"C:\Users\shoba_eswar\Documents\REquest.pdf", System.IO.FileMode.Open);
    response.Content = new StreamContent(stream);
    response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
    response.Content.Headers.ContentDisposition.FileName = "NewTab";
    response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/pdf");
    return response;
}

但是它只返回JSON响应:

{
   "version":{
      "major":1,
      "minor":1,
      "build":-1,
      "revision":-1,
      "majorRevision":-1,
      "minorRevision":-1
   },
   "content":{
      "headers":[
         {
            "key":"Content-Disposition",
            "value":[
               "attachment; filename=NewTab"
            ]
         },
         {
            "key":"Content-Type",
            "value":[
               "application/pdf"
            ]
         }
      ]
   },
   "statusCode":200,
   "reasonPhrase":"OK",
   "headers":[

   ],
   "requestMessage":null,
   "isSuccessStatusCode":true
}

我做错什么了吗?

推荐答案

正如在ASP.NET Core HTTPRequestMessage returns strange JSON message中所解释的,ASP.NET Core不支持返回HttpResponseMessage(您安装了哪个包来访问该类型?).

因此,序列化程序只需将HttpResponseMessage的所有公共属性写入输出,就像其他任何不受支持的响应类型一样.

要支持自定义响应,必须返回一个类型.有plenty of those个.在你的情况下,我会研究FileStreamResult:

public IActionResult Get(int id)
{
    var stream = new FileStream(@"path\to\file", FileMode.Open);
    return new FileStreamResult(stream, "application/pdf");     
}

或者只需使用PhysicalFileResult,即可为您处理流:

public IActionResult Get(int id)
{
    return new PhysicalFileResult(@"path\to\file", "application/pdf");
}

当然,所有这些都可以使用帮助器方法简化,例如Controller.File():

public IActionResult Get(int id)
{
    var stream = new FileStream(@"path\to\file", FileMode.Open);
    return File(stream, "application/pdf", "FileDownloadName.ext");
}

这只是抽象了FileContentResultFileStreamResult的创建(对于这个重载,后者).

或者,如果您正在转换旧的MVC或Web API应用程序,并且不想同时转换所有代码,请添加对WebApiCompatShim (NuGet)的引用,并将当前代码包装在ResponseMessageResult中:

public IActionResult Get(int id)
{
    var response = new HttpResponseMessage(HttpStatusCode.OK);           
    var stream = ...
    response.Content...

    return new ResponseMessageResult(response);
}

如果不想使用return File(fileName, contentType, fileDownloadName),那么FileStreamResult不支持从构造函数或通过属性设置内容处置头.

在这种情况下,在返回文件结果之前,您必须自己将响应头添加到响应中:

var contentDisposition = new ContentDispositionHeaderValue("attachment");
contentDisposition.SetHttpFileName("foo.txt");
Response.Headers[HeaderNames.ContentDisposition] = contentDisposition.ToString();

Asp.net相关问答推荐

ASP.NET MVC项目中的产品采购并发性测试

禁用托管优化并重新启动调试实际上在 Visual Studio 中更改了哪些设置?

如何在asp.net中判断会话是否过期

将复选框的值传递给 asp.net mvc4 中的控制器操作

Web Api 请求内容在操作过滤器中为空

Twitter bootstrap glyphicons 不会出现在发布模式 404 中

如何在 Visual Studio 2017 中使用 NPM 并安装包?

通过 jQuery 调用 ASP.NET 服务器端方法

从后面的 asp.net 代码中读取表单身份验证 cookie

如何将枚举类型绑定到 DropDownList?

如何从后面的 ASP.NET 代码访问 HTML 表单输入

单独装配和路由中的控制器

如何使用 WebRequest 发布数据并从网页获取响应

svg 无法在 localhost 上的 IIS 网络服务器上运行

MVC 4 - Razor - 将变量传递到 href url

IIS 是否执行非法字符替换?如果是这样,如何阻止它?

DropDownList 的 SelectedValue 与 SelectedItem.Value

哪个事件先调用?母版页 Page_Load 或内容页 Page_Load

如何在不遍历每个循环的情况下从字典对象中获取所有键(仅键)

SignalR 2.0 错误:无法加载文件或程序集 Microsoft.Owin.Security