1

我有一个 ASP.NET CORE 应用程序,它向 REST(Orthanc Rest API)发送 POST/GET 请求。问题是我收到结果并将其转换为 JSON,但邮递员显示为空数组。这是我的代码:

// GET Method
public class PACSController : ControllerBase
    {

        // GET: api/PACS
        [HttpGet]
        public async Task<object> Get()
        {
            var result = await Orthanc.Orthanc.InstanceAsync();           
            return result;
        }
    }



public class Orthanc
    {
        public static string baseUrl = "https://demo.orthanc-server.com/";
        public static async Task<object> InstanceAsync()
        {

            string url = baseUrl + "instances";
            using (HttpClient client = new HttpClient())

            using (HttpResponseMessage res = await client.GetAsync(url))

            using (HttpContent content = res.Content)
            {
                string data = await content.ReadAsStringAsync();
                if (data != null)
                {
                    Console.WriteLine(data);
                }

                var jData = JsonConvert.DeserializeObject(new string[] { data }[0]);

                return jData;
            }

        }
    }

代码中请求的结果

邮递员结果

4

1 回答 1

1

作为改进 ASP.NET Core 共享框架工作的一部分,Newtonsoft.Json已从 ASP.NET Core 3.x 的 ASP.NET Core 共享框架中删除。

按照步骤:

Microsoft.AspNetCore.Mvc.NewtonsoftJson在nuget上安装包。

Install-Package Microsoft.AspNetCore.Mvc.NewtonsoftJson

更新 Startup.ConfigureServices 以调用AddNewtonsoftJson.

services.AddControllersWithViews().AddNewtonsoftJson();

参考:

https://docs.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-3.1&tabs=visual-studio#use-newtonsoftjson-in-an-aspnet-core-30 -mvc-项目

于 2020-05-15T03:26:48.653 回答