在类帐户中,属性行业是枚举类型行业的集合
一个账号可以属于多个行业。
public class Account
{
[JsonProperty(ItemConverterType = typeof(StringEnumConverter))]
public List<Industry> Industrys { get; set; }
}
Public enum Industry
{
AerospaceAirlines = 0,
Agriculture = 1,
Apparel = 2,
Automotive = 3,
Banking = 4,
BioTechnology = 5,
Chemicals = 6,
Communications = 7,
Construction = 8,
Consultancy = 9,
ConsumerDurables = 10,
Education = 11,
}
新建账号时,不强制添加行业。当请求对象中未提供任何值时,此行业的属性将失败。
反序列化时出现错误“值不能为空。\r\n参数名称:源”
当默认绑定器试图填充accountModel对象时,就会发生故障。
[HttpPost]
public async Task<IActionResult> Post([FromBody]AccountModel accountModel)
{
if (accountModel == null)
{
throw new ApiException(ApplicationErrorCode.FieldRequiredError, "request cannot be empty");
}
}
在没有提供行业的情况下,如何允许发件人发送请求?
发布于 2019-11-05 23:46:54
更改:
public class Account
{
[JsonProperty(ItemConverterType = typeof(StringEnumConverter))]
public List<Industry> Industrys { get; set; }
}
至:
public class Account
{
[JsonProperty(ItemConverterType = typeof(StringEnumConverter))]
public List<Industry> Industrys { get; set; } = new List<Industry>();
}
https://stackoverflow.com/questions/58714794
复制相似问题