如何将空字符串发送到 int params 错误

How to send empty string to int params error

我有一个带有 int 参数的端点

[HttpGet]
public IActionResult Test(int a=-1)
{
    return Ok();
}

当我尝试向该资源发送空字符串时,我收到了 400。 如何获得默认值 -1 ?

{
    "errors": {
        "a": [
            "The value '' is invalid."
        ]
    },
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
    "title": "One or more validation errors occurred.",
    "status": 400,
    "traceId": "00-bb8f5c4f34041e4a9c9ccbc241e711fa-f378875df6f8c541-00"
}

您可以将 [Route("{a?}")] 添加到您的终结点,它将忽略非数值作为空字符串。