I have this post method in my controller
[HttpPost]
public Task<AddClientAppSettingResponse> Post(AddClientAppSettingCommand mysetting)
=> Mediator.Send(mysetting);
My post method accept an AddClientAppSettingCommand
model as you can see:
public class AddClientAppSettingCommand : IRequest<AddClientAppSettingResponse>
{
public ClientAppSettings Setting { get; set; }
public AddClientAppSettingCommand(ClientAppSettings setting)
{
Setting = setting;
}
}
Here are my model classes:
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; }
}
But when I call my API using postman with these values
{
"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
}
}
I get this error
Each parameter in the deserialization constructor on type 'domain.Entities.ClientAppSettings' must bind to an object property or field on deserialization. Each parameter name must match with a property or field on the object. The match can be case-insensitive
As a note I use MediatR for CQRS ,I followed this but it didn't work .I put a [JsonConstructor]
on
public AddClientAppSettingCommand(ClientAppSettings setting)
{
Setting = setting;
}
But it didn't work .and my variable setting
is null
CodePudding user response:
you need a parameterless constructor for api input parameter
public class AddClientAppSettingCommand : IRequest<AddClientAppSettingResponse>
{
public ClientAppSettings Setting { get; set; }
public AddClientAppSettingCommand() {}
public AddClientAppSettingCommand(ClientAppSettings setting)
{
Setting = setting;
}
}
and the same for settings
public class ClientAppSettings : BaseEntity
{
public ClientAppSettings(){}
}