1

I have an ASP.NET CORE application that sends a POST/GET request to a REST (Orthanc Rest API). The issue is I receive the result and convert it to a JSON, but postman shows as an empty array. here is my code:

// 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;
            }

        }
    }

The result of request inside the code

Postman result

  • Did you use asp.net core 3.x?I could not reproduce the issue unless using asp.net core 3.x. – Rena May 15 '20 at 03:31

1 Answers1

1

As part of the work to improve the ASP.NET Core shared framework, Newtonsoft.Json has been removed from the ASP.NET Core shared framework for asp.net core 3.x.

Follow the steps:

Install the Microsoft.AspNetCore.Mvc.NewtonsoftJson package on nuget.

Install-Package Microsoft.AspNetCore.Mvc.NewtonsoftJson

Update Startup.ConfigureServices to call AddNewtonsoftJson.

services.AddControllersWithViews().AddNewtonsoftJson();

Reference:

https://learn.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-project

Rena
  • 30,832
  • 6
  • 37
  • 72