“Entities”类型的反序列化构造函数中的每个参数必须绑定到反序列化时的对象属性或字段

Ehs*_*bar 4 c# json controller mediatr .net-6.0

我的控制器中有这个 post 方法

[HttpPost]
public Task<AddClientAppSettingResponse> Post(AddClientAppSettingCommand mysetting)
    => Mediator.Send(mysetting);
Run Code Online (Sandbox Code Playgroud)

我的帖子方法接受AddClientAppSettingCommand模型,如您所见:

public class AddClientAppSettingCommand : IRequest<AddClientAppSettingResponse>
{
        public ClientAppSettings Setting { get; set; }

        public AddClientAppSettingCommand(ClientAppSettings setting)
        {
            Setting = setting;
        }
}
Run Code Online (Sandbox Code Playgroud)

这是我的模型类:

public class ClientAppSettings : BaseEntity
{
        public ClientAppSettings(string userId) : base($"ClientAppSettings/{userId}")
        {
        }

        public bool LightTheme { get; set; }
        public OrderSettings Order { get; set; }
        public NotchSettings Notch { get; set; }
        public int PageSize { get; set; }
        public bool ApplyCommissionInPortfolio { get; set; }
        public bool UseClosingPriceInPortfolioTotalValue { get; set; }
        public bool ShowNotifications { get; set; } = true;
        public bool NoSleep { get; set; } = true;
        public bool NoBalance { get; set; } = false;
        public bool DataTracker { get; set; } = false;
        public bool UserStatusBarToUp { get; set; } = false;
        public bool PortfolioBasedOnLastPositivePeriod { get; set; } = false;
}

public class OrderSettings
{
        public long BuyQuantity { get; set; }
        public long SellQuantity { get; set; }
        public float Tick { get; set; }
        public string TickType { get; set; }
        public bool PriceFromHeadline { get; set; }
        public bool OrderConfirmation { get; set; }
        public bool DivideOrderToMultiple { get; set; }
}

public class NotchSettings
{
        public bool Up { get; set; }
        public bool Down { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

但是当我使用邮递员使用这些值调用我的 API 时

{
   "setting":{
      "LightTheme":true,
      "Order":{
         "BuyQuantity":"1",
         "SellQuantity":"1",
         "Tick":"1.0",
         "TickType":"asas",
         "PriceFromHeadline":true,
         "OrderConfirmation":true,
         "DivideOrderToMultiple":true
      },
      "Notch":{
         "Up":true,
         "Down":true
      },
      "PageSize":"10",
      "ApplyCommissionInPortfolio":true,
      "UseClosingPriceInPortfolioTotalValue":true,
      "ShowNotifications":true,
      "NoSleep":true,
      "NoBalance":true,
      "DataTracker":true,
      "UserStatusBarToUp":true,
      "PortfolioBasedOnLastPositivePeriod":true
   }
}
Run Code Online (Sandbox Code Playgroud)

我收到这个错误

类型“domain.Entities.ClientAppSettings”的反序列化构造函数中的每个参数必须绑定到反序列化时的对象属性或字段。每个参数名称必须与对象上的属性或字段匹配。匹配可以不区分大小写

作为一个注释,我使用 MediatR 进行 CQRS,我遵循了这个,但它不起作用。我放了[JsonConstructor]一个

 public AddClientAppSettingCommand(ClientAppSettings setting)
        {
            Setting = setting;
        }
Run Code Online (Sandbox Code Playgroud)

但它不起作用。而且我的变量setting为空

Ser*_*rge 11

你需要一个无参数构造函数作为 api 输入参数

public class AddClientAppSettingCommand : IRequest<AddClientAppSettingResponse>
{
        public ClientAppSettings Setting { get; set; }

        public AddClientAppSettingCommand() {}
       

        public AddClientAppSettingCommand(ClientAppSettings setting)
        {
            Setting = setting;
        }
}
Run Code Online (Sandbox Code Playgroud)

和设置相同

public class ClientAppSettings : BaseEntity
{
public ClientAppSettings(){}
      
}
 
Run Code Online (Sandbox Code Playgroud)