C# 将PDF结果传递给ASP.NET Core中的另一个web api

C# 将PDF结果传递给ASP.NET Core中的另一个web api,c#,pdf,asp.net-web-api,asp.net-core-webapi,C#,Pdf,Asp.net Web Api,Asp.net Core Webapi,我有一个web api,它检索存储在数据库中的PDF文件 [HttpGet("file/{id}")] public IActionResult GetFile(int id) { var file = dataAccess.GetFileFromDB(id); HttpContext.Response.ContentType = "application/pdf"; FileContentResult result = new FileContentResult(fi

我有一个web api,它检索存储在数据库中的PDF文件

[HttpGet("file/{id}")]
public IActionResult GetFile(int id)
{
    var file = dataAccess.GetFileFromDB(id);

    HttpContext.Response.ContentType = "application/pdf";
    FileContentResult result = new FileContentResult(file, "application/pdf")
    {
        FileDownloadName = "test.pdf"
    };

    return result;
}
我需要编写一个包装器web api,它将上述web api的结果传递给客户端

使用.NETCore实现这一目标的最佳方法是什么?我是否应该从上述web api返回字节数组,并在包装器api中将字节数组转换为FileContentResult

任何代码示例都会非常有用


谢谢。

您可以尝试将响应从
HttpClient
重新路由到用户:

using System.Net.Http;
using Microsoft.AspNetCore.Mvc;

public class TestController : Controller
{
    public async Task<IActionResult> Get()
    {
        using (var client = new HttpClient())
        {
            var response = await client.GetAsync("http://apiaddress", HttpCompletionOption.ResponseHeadersRead); // this ensures the response body is not buffered

            using (var stream = await response.Content.ReadAsStreamAsync()) 
            {
                return File(stream, "application/pdf");
            }
        }
    }
}
使用System.Net.Http;
使用Microsoft.AspNetCore.Mvc;
公共类TestController:控制器
{
公共异步任务Get()
{
使用(var client=new HttpClient())
{
var response=wait client.GetAsync(“http://apiaddress,HttpCompletionOption.ResponseHeadersRead);//这确保响应正文未被缓冲
使用(var stream=await response.Content.ReadAsStreamAsync())
{
返回文件(流,“应用程序/pdf”);
}
}
}
}

您实际上需要代理吗?是的,有点。客户端(Angular 2)调用包装器web api,该api在内部调用db web api来检索数据。使用.NET Core最简单的方法是用作实现的基础(它使用中间件)。或者有理由重新实现另一个API?包装器API也负责一些其他事情,所以很遗憾我不得不使用包装器API。谢谢,我会尝试一下。只是想知道,它会把PDF内容读两遍吗?i、 一个在db api级别,另一个在包装器api级别。当然可以,但是第二次它应该直接流式传输到客户端。