将 PDF 结果传递到 ASP.NET Core 中的另一个网站 api

Pass PDF result to another web api in ASP.NET Core

我有一个网络 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 的结果传递给客户端。

使用 .NET Core 实现此目的的最佳方法是什么?我应该 return 来自上述网络的字节数组 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");
            }
        }
    }
}