【问题标题】:Response "',' is an invalid start of a value. Path: $. in Asp.Net Core API响应“',' 是值的无效开始。路径:$。在 Asp.Net Core API 中
【发布时间】:2020-10-29 18:59:13
【问题描述】:

我在 Asp.Net Core 3.1 中开发 API,下面有一个 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."
        ]
    }
}

【问题讨论】:

    标签: c# api asp.net-core asp.net-web-api asp.net-core-webapi


    【解决方案1】:
    [RegularExpression("^[1-9]\\d*$", ErrorMessage = "Invalid fieldName.")]
    public int countryId { get; set; }
    

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

    "countryId": null ,
    

    【讨论】:

    • 我说的是这个案子,如果我收到"countryId": ,这样的请求怎么办
    • 其实我从来没有看到前端开发者的请求没有设置指定字段的值,否则他会删除整个字段。尝试从您的 json 中删除该字段
    • 假设您对请求没有任何控制权,并且客户端正在向您发送此数据
    • 亲爱的你不能把“countryId”留空,你必须给它设置一个值
    • 那么请联系你的客户,和他商量一下
    【解决方案2】:

    请求的 json 不正确:您必须传递一些东西作为国家/地区 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"
            }
        ]
    }
    

    【讨论】:

    • 假设您对请求没有任何控制权,并且客户端正在向您发送此数据
    • 那么客户端将不会得到任何响应,但会出现该错误,因为 json 不正确。客户端至少应该发送一个语法正确的 json。
    猜你喜欢
    • 1970-01-01
    • 2021-08-13
    • 2019-09-19
    • 2019-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-15
    • 1970-01-01
    相关资源
    最近更新 更多