AspnetCore datacontract 在 JSON 中使用我的字典键的名称空间序列化

AspnetCore datacontract serialize in JSON with the name namespace of the keys of my dictionary

我试图发送字典。 这是我的字典的结构:

var gridsAndCountriesByShipper = new Dictionary<Organisation, List<Grid>>();

这是命名空间而不是对象值出现的class:

namespace ez.webservice.PublicRestApi.DTO.References
{
    [DataContract(Name = "organisation", Namespace = "")]
    public class Organisation
    {
        [DataMember(Name = "id")]
        public int Id { get; set; }

        [DataMember(Name = "longName")]
        public string LongName { get; set; }

        [DataMember(Name = "shortName")]
        public string ShortName { get; set; }
    }
}

最后这是我发送这个对象时的结果:

     return Ok(gridsAndCountriesByShipper);

{
  "ez.webservice.PublicRestApi.DTO.References.Organisation": [
    {
      "owner": {
        "$id": "1",
        "id": 2,
        "longName": null,
        "shortName": "Test"
      },
      "calendar": {
        "id": 3,
        "name": null,
        "dotNetName": null,
        "mySqlName": null,
        "offset": 0
      },
      "country": {
        "id": 13,
        "name": "France",
        "alpha2": "FR",
        "alpha3": "FRA",
        "grids": [],
        "logo": {
          "id": 13,
          "name": "France",
          "shortName": "FR"
        }
      },
      "countryFlag": null,
      "flag": "",
      "granularity": "DAY",

我没有找到解决办法。

您的 Organisation 不会覆盖 ToString() 方法,默认行为是 return class 具有命名空间

的名称