【发布时间】:2021-04-16 20:43:35
【问题描述】:
在一个 ASP.NET 核心项目中,我已从使用 ResourceModel 切换到使用 JObject 作为我的 [FromBody] 参数。然后,我将JObject.ToObject<ResourceModel>() 传递给服务,但希望为ContainsKey 和类似功能维护JObject 本身。
如果JObject.ToObject<T> 成功,我可以使用TryValidateModel(),如果失败,就简单地返回BadRequest(ModelState)。但是,我遇到的问题是JObject.ToObject<T> 引发异常时 - 我不确定如何捕获ModelState 中的序列化错误。
例子:
public class Person {
public string FirstName {get;set;}
public string LastName {get;set;}
public uint Age {get;set;}
}
public class CommonParameters {
[FromQuery] public string? Fields {get;set;}
public string UserName {get;set;}
public IPAddress? RequestorIp {get;set;}
public JObject? JsonBody {get;set;}
}
public class PersonController : ControllerBase {
private readonly PersonService _personService;
// this is how I had been doing it
[HttpPatch("obsolete/{id}")] // this method doesn't actually exist, but is shown for the example
public async Task<IActionResult> UpdatePerson([FromRoute] int id, [FromBody] Person parameters, [FromHeader] CommonParameters commonParameters) {
SetCommonParameters(commonParameters, parameters);
// if parameters.Age in the json body was a string, BadRequest is returned before this method even starts
_personService.Set(parameters);
await _personService.SaveChangesAsync();
return NoContent();
}
// this is how I'm trying to do it now, so that I only update Age if JObject.ContainsKey("Age")
// instead of parameters.Age != default, etc.
[HttpPatch("{id}")]
public async Task<IActionResult> UpdatePerson([FromRoute] int Id, [FromBody] JObject parameters, [FromHeader] CommonParameters commonParameters) {
SetCommonParameters(commonParameters, parameters);
// if parameters.Age is a string in this version, a JsonReaderException
// or JsonSerializationException is thrown when I call JObject.ToObject<>()
// and I have to validate the model separately after that
// what this means is, a BadRequest with the ModelState errors is not
// returned if there's an issue with the json.
var model = parameters.ToObject<Person>();
if (!TryValidateModel(model)) return BadRequest(ModelState);
_personService.Set(person);
await _personService.SaveChangesAsync();
return NoContent();
}
private void SetCommonParameters(CommonParameters commonParameters, JObject? jsonBody = null) {
commonParameters.JsonBody = jsonBody;
commonParameters.UserName = User.Identity?.Name;
commonParameters.RequestorIp = Request.HttpContext.Connection.RemoteIpAddress;
}
}
编辑:正如 Guru Stron 所要求的,这是一个无效模型/json 对象的示例。
型号:
public class ContactInfo {
// PhoneNumber being a struct representing an 11-digit phone number
// PhoneType being an enum of Cell, Work, Home, etc.
public Dictionary<PhoneType, PhoneNumber[]> PhoneNumbers {get;set;} = new();
public string? Apartment {get;set;}
public int AddressNumber {get;set;}
public string StreetName {get;set;}
public string City {get;set;}
// State being an enum of the 50 United States of America
public State State {get;set;}
}
无效的json
{
"phoneNumbers": {
"work": [ "1-111-111-1111" ],
"home": [ "2-222-222-2222" ],
"cell": [ "1-800-CALL-NOW" ]
},
"apartment": null,
"addressNumber": "3a",
"streetName": "Imagination St.",
"city": "Atlantis",
"state": "Atlantic"
}
在这种情况下,将ContactInfo 的上述json 传递给请求ContactInfo 参数的控制器方法将返回一个错误请求,指示状态无效,地址编号无效,和 电话号码无效 - 没有专门对此进行测试,如下所示。
{
"errors": {
"phoneNumbers": [
"Could not parse phone number \"1-800-CALL-NOW\". The value must be an 11-digit numeric value."
],
"addressNumber": [
"The value could not be parsed as int."
],
"state": [
"\"Atlantic\" is an invalid value for State. The following values are valid: ..."
]
}
}
如果控制器请求JObject,然后我调用JObject.ToObject 并捕获异常,我将得到一个这些错误 - 例如Could not parse phone number "1-800-CALL-NOW". The value must be an 11-digit numeric value. 其他错误将被忽略直到那个问题被修复并且用户再次尝试。
【问题讨论】:
-
您可以使用
ModelState.AddModelError("", ...)捕获异常并将其文本添加为模型级错误 -
不幸的是,这只会捕获一个异常——第一个解析错误——而不是所有可能发生的异常。
-
您能否提供一些无效 json 和预期错误输出的示例。
-
@GuruStron 我已更新帖子以在底部包含一个示例。
-
你的意思是你想得到ModelState的所有错误吗?如果是这样,你可以使用
foreach (var item in ModelState)来查询ModelState的错误。
标签: c# json asp.net-core json.net bad-request