从Body和FromQuery属性因HTTPPOST而失败,返回“一个或多个验证错误发生”。

发布于 2025-02-09 05:45:49 字数 907 浏览 3 评论 0原文

请参阅我的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事与诗 2025-02-16 05:45:49

该代码没有到达控制器,因为ASP.NET Core运行了验证,以断言请求中的数据有效,并且可以映射到操作的参数。将数据发送到ASP.NET核心时,重要的是,数据结构符合操作的期望,如果不是,则框架返回BadRequest响应(状态代码400)。

在您的代码中,查询参数被命名为list,在角度代码中,您将其发送为scopes。这些应该匹配,因此您应该重命名其中一个(或使用FromQuery的构造函数参数将名称设置为scopes)。

此外,请删除查询参数的json.stringify,然后分配数组。然后,URL将具有scopes的几个值,例如:

/mycontroller/?scopes=1&scopes=3&scopes=5

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 as scopes. These should match, so you should rename one of them (or use the constructor parameter of FromQuery to set the name to scopes).

In addition, remove the JSON.stringify for the query parameter and just assign the array. The url will then have several values for scopes, e.g.:

/mycontroller/?scopes=1&scopes=3&scopes=5

ASP.NET Core Web API parses the URI in a different way and will assign the values to the scopes parameter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文