【发布时间】:2020-09-08 11:26:30
【问题描述】:
我正在尝试创建一个带有 4 个可选参数的 GET-Route。没有必需的参数。
我的路线是这样的
[Produces("application/json")]
[HttpGet("SearchWhatever", Name = "GetWhatever")]
public IEnumerable<TmpObject> SearchWhatever(long? eid= null, long? pid = null, string name= null, string firstname= null)
{
//do Smth
}
基本上,“eid”和“pid”按预期工作,它们完全是可选的。但是,这些字符串不能作为“可选”工作。
如果我调用“../SearchWhatever?eid=6610232513694”之类的 API,我将收到以下错误:
{
errors: {
name: [
"The name field is required."
],
firstname: [
"The firstname field is required."
]
},
type: "https://tools.ietf.org/html/rfc7231#section-6.5.1",
title: "One or more validation errors occurred.",
status: 400,
traceId: "00-e4eb5dc9bb266e44abda734d6a411e44-5f5a40de7fc10540-00"
}
我如何实现我的目标?甚至可能吗?我认为给字符串一个像 null 这样的默认值会使参数成为可选参数。
提前致谢
【问题讨论】:
-
首先,你没有'ean'查询参数。
-
已修复,只是一个错字(在我的代码中是正确的)。
-
尝试使用 "" 作为字符串参数的默认值。 ("... string name="", string firstname=""... ")
-
已经尝试过了,但会产生同样的错误。
-
哪个版本的asp.net core? 3.1?
标签: asp.net asp.net-core webapi