拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 “物体”型别的反序列化建构式中的每个自变量都必须系结到反序列化时的物件属性或栏位

“物体”型别的反序列化建构式中的每个自变量都必须系结到反序列化时的物件属性或栏位

白鹭 - 2022-02-10 2066 0 0

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

[HttpPost]
public Task<AddClientAppSettingResponse> Post(AddClientAppSettingCommand mysetting)
    => Mediator.Send(mysetting);

AddClientAppSettingCommand如您所见,我的 post 方法接受一个模型:

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

        public AddClientAppSettingCommand(ClientAppSettings setting)
        {
            Setting = setting;
        }
}

这是我的模型类:

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

但是当我使用具有这些值的邮递员呼叫我的 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
   }
}

我收到这个错误

作为一个说明我用MediatR的CQRS,我跟着这个,但没有奏效。我把[JsonConstructor]

 public AddClientAppSettingCommand(ClientAppSettings setting)
        {
            Setting = setting;
        }

但它没有用。我的变量setting为空

uj5u.com热心网友回复:

您需要一个用于 api 输入自变量的无自变量建构式

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

        public AddClientAppSettingCommand() {}
       

        public AddClientAppSettingCommand(ClientAppSettings setting)
        {
            Setting = setting;
        }
}

和设定相同

public class ClientAppSettings : BaseEntity
{
public ClientAppSettings(){}
      
}
 
标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *