从Body和FromQuery属性因HTTPPOST而失败,返回“一个或多个验证错误发生”。
请参阅我的C#核心API代码如下:
[HttpPost]
public bool Save([FromBody] string data,[FromQuery] ICollection<int> list)
{
var result = true;
return result;
}
和Angular Post Call如下:
postAction(data: any, list: number[]): Observable<any> {
let params = new HttpParams().set('scopes', JSON.stringify(list));
return this.httpClient.post( `${this.apiUri}/Save`,data, { params });
}
我会收到“一个或多个验证错误”的错误。
使用这些验证,
更新1:
代码详细信息 400 无证件 错误: 响应主体
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "00-b6e1d156b5ff1940a6186060c4902663-36e8ff61a6d6374b-00",
"errors": {
"$": [
"'s' is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0."
]
}
}
Please see my c# core api code as following:
[HttpPost]
public bool Save([FromBody] string data,[FromQuery] ICollection<int> list)
{
var result = true;
return result;
}
and angular post call as following:
postAction(data: any, list: number[]): Observable<any> {
let params = new HttpParams().set('scopes', JSON.stringify(list));
return this.httpClient.post( `${this.apiUri}/Save`,data, { params });
}
with these i'm getting an "One or more validation errors occurred" error with status code 400.
can't we use FromBody and FromQuery at same time ?
Update 1:
Code Details
400
Undocumented
Error:
Response body
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "00-b6e1d156b5ff1940a6186060c4902663-36e8ff61a6d6374b-00",
"errors": {
"quot;: [
"'s' is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0."
]
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该代码没有到达控制器,因为ASP.NET Core运行了验证,以断言请求中的数据有效,并且可以映射到操作的参数。将数据发送到ASP.NET核心时,重要的是,数据结构符合操作的期望,如果不是,则框架返回BadRequest响应(状态代码400)。
在您的代码中,查询参数被命名为
list
,在角度代码中,您将其发送为scopes
。这些应该匹配,因此您应该重命名其中一个(或使用FromQuery
的构造函数参数将名称设置为scopes
)。此外,请删除查询参数的
json.stringify
,然后分配数组。然后,URL将具有scopes
的几个值,例如:ASP.NET Core Web API以不同的方式解析URI,并将值分配给
scopes
参数。The code does not reach the controller because ASP.NET Core runs a validation to assert that the data in the request are valid and can be mapped to the parameters of your action. When sending data to ASP.NET Core, it is important that the structure of the data meets the expectations of the action, if not, the framework returns a BadRequest response (status code 400).
In your code, the query parameter is named
list
, in the Angular code, you send it asscopes
. These should match, so you should rename one of them (or use the constructor parameter ofFromQuery
to set the name toscopes
).In addition, remove the
JSON.stringify
for the query parameter and just assign the array. The url will then have several values forscopes
, e.g.:ASP.NET Core Web API parses the URI in a different way and will assign the values to the
scopes
parameter.