响应 "'","" 是值的无效开头。路径: $。在 Asp.Net 核心 API 中



我正在Asp中开发API。Net Core 3.1,我在下面有一个POST方法(内容类型为application/JSON(,我故意向响应传递一个无效的JSON,响应也很清楚。但我的问题是,我可以像一样做出更改以返回响应吗

countryId是的必填字段

对于这种特殊情况。如果可以的话,请告诉我,否则,我也可以接受这个响应(因为这个响应也是有效的,首先检查内容类型是否是有效的JSON(。

方法:

public ActionResult ValidateFields(ValidateFieldsRequest validateFieldsRequest)
{

请求类别:

public class ValidateFieldsRequest
{
//string currencyCode, int countryId, string fieldName, string fieldValue
[Required]
public string currencyCode { get; set; }
[Required]
[RegularExpression("^[1-9]\d*$", ErrorMessage = "Invalid fieldName.")]
public int countryId { get; set; }
[Required]
[MinLength(1, ErrorMessage = "At least one field required")]
public List<Field> fields { get; set; }
}

请求:

{
"currencyCode": "RUB",
"countryId": ,
"fields": [{
"fieldName": "BIC or Bank Code",
"fieldValue": "12345678901234567"
},
{
"fieldName": "Beneficiary Account Number",
"fieldValue": "123456"
}
]
}

响应:

{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "|6a64d7ad-495850db788356cd.",
"errors": {
"$.countryId": [
"',' is an invalid start of a value. Path: $.countryId | LineNumber: 2 | BytePositionInLine: 28."
]
}
}
[RegularExpression("^[1-9]\d*$", ErrorMessage = "Invalid fieldName.")]
public int countryId { get; set; }

上面设置了错误消息,但实际上您发送的json不能像这样发送,您应该设置

"countryId": null ,

请求的json不正确:您必须以country-id:的形式传递一些内容

{
"currencyCode": "RUB",
"countryId": "",
"fields": [{
"fieldName": "BIC or Bank Code",
"fieldValue": "12345678901234567"
},
{
"fieldName": "Beneficiary Account Number",
"fieldValue": "123456"
}
]
}

{
"currencyCode": "RUB",
"countryId": null,
"fields": [{
"fieldName": "BIC or Bank Code",
"fieldValue": "12345678901234567"
},
{
"fieldName": "Beneficiary Account Number",
"fieldValue": "123456"
}
]
}

相关内容

  • 没有找到相关文章

最新更新