简体   繁体   English

ASP.Net 核心 - 返回 JObject 属性时的空白响应

[英]ASP.Net core - blank response when returning a JObject property

I am using ASP.Net core 5.0 and want to return IEnumerable of an object as Action method response.我正在使用 ASP.Net core 5.0 并希望将对象的 IEnumerable 作为 Action 方法响应返回。

Here is the response class:这是响应类:

public class TestResponse
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Newtonsoft.Json.Linq.JObject PayLoad { get; set; }
}

This is my Action method:这是我的动作方法:

[HttpGet]
public IEnumerable<TestResponse> TestRequest()
{
    var testResponses = new List<TestResponse>();
    testResponses.Add(new TestResponse { Id = 10, Name = "Name1", PayLoad = JObject.FromObject(new { Status = "Success", Message = "Working good, take care!"})});
    testResponses.Add(new TestResponse { Id = 11, Name = "Name2", PayLoad = JObject.FromObject(new { Status = "Success", Message = "Working good, take care!" }) });

    return testResponses;
}

When I run this, the response I see for PayLoad field is:当我运行它时,我看到的 PayLoad 字段的响应是:

[
  {
    "id": 10,
    "name": "Name1",
    "payLoad": {
      "Status": [],
      "Message": []
    }
  },
  {
    "id": 11,
    "name": "Name2",
    "payLoad": {
      "Status": [],
      "Message": []
    }
  }
]

Why are the Status and Message fields blank?为什么StatusMessage字段为空白? What am I missing?我错过了什么?

Please add package like below:请添加如下软件包:

<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="5.0.17" />

And your ConfigureServices method like below:您的 ConfigureServices 方法如下所示:

public void ConfigureServices(IServiceCollection services)
{
        
    services.AddControllersWithViews().AddNewtonsoftJson();
}

And it works for me.它对我有用。

在此处输入图像描述

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM