简体   繁体   English

.NET Core 3.1 WebApi - 来自另一个 WebApi 的直通 JSON 响应

[英].NET Core 3.1 WebApi - Passthrough JSON response from another WebApi

I have the following setup:我有以下设置:

  • WebApi #1 - Returns JSON via an OkResult class WebApi #1 - 通过OkResult class 返回 JSON
  • WebApi #2 - Returns JSON via an OkResult class WebApi #2 - 通过OkResult class 返回 JSON

WebApi #2 needs to call WebApi #1 and simply pass the JSON result back via its OkResult. WebApi #2 需要调用 WebApi #1 并通过其 OkResult 将 JSON 结果传回。 The issue I have is if I do that, I end up with escaped JSON in the response:我遇到的问题是,如果我这样做,我最终会在响应中逃脱 JSON :

"{\"id\":1,\"key\":\"value\",\"key\":\"value\",\"key\":\"value\",\"key\":\"value\"}"

What is the best way to just passthrough this return?什么是通过此回报的最佳方式?

UPDATE:更新:

Here is the code that makes the API call to WebApi #1:这是使 API 调用 WebApi #1 的代码:

public async Task<string> Get(string uri)
    {
        try
        {
            string responseBody = await _client.GetStringAsync(uri);
            return responseBody;
        }
        catch (HttpRequestException ex)
        {
            _logger.LogError(ex.Message);
            return null;
        }
    }

And here is the return in WebApi #2:这是 WebApi #2 中的返回值:

public async Task<IActionResult> Get(string data)
    {
        var uri = String.Format(Environment.GetEnvironmentVariable("ENDPOINT"), data);
        var result = await _client.Get(uri);
        return Ok(result);
    }

You need to do this:你需要这样做:

return Content(result, "application/json");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET Core 3.1:如何将驼峰 json 从 webapi 反序列化为 pascalcase - ASP.NET Core 3.1: How to deserialize camelcase json from webapi to pascalcase WebApi与.NET核心和json验证 - WebApi with .NET core and json validation 将文件从另一个 webapi 传递到另一个 asp.net core webapi - Pass files to another asp.net core webapi from another webapi C# ASP.NET WebApi (2/Core 3.1) 大数的 Json 序列化问题 - C# ASP.NET WebApi (2/Core 3.1) Problem with Json serialization of big numbers 如何生成一个 Url 到另一个 controller WebApi core 3.1 - How to generate a Url to another controller WebApi core 3.1 如何在 .NET Core 3.1 WebAPI 中托管 Angular 应用程序? - How to host an Angular app inside .NET Core 3.1 WebAPI? WebApi .net core 3.1 - 如何自定义null发帖验证信息? - WebApi .net core 3.1 - How to customize validation message for null posting? .Net Core 3.1 WebAPI 如何序列化 IEnumerable<t> 来自 POST 正文</t> - .Net Core 3.1 WebAPI How to serialize IEnumerable<T> from POST body 尚未完成对来自WebApi Core的Include的回复 - Not completed response with Include from WebApi Core Swagger UI无法显示来自ASP.net Core WebAPI的正确响应 - Swagger UI unable to show proper response from ASP.net Core WebAPI
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM