Angular 7 HttpClient将请求参数发送到端点

问题描述 投票:1回答:1

我有一个使用此Put请求方法的Angular服务:

put(peopleFieldID: number, peopleFieldName: string): Observable<number> {
let params = new HttpParams();
params = params.append("peopleFieldID", peopleFieldID.toString());
params = params.append("peopleFieldName", peopleFieldName);

return this.http
  .put<number>(this.url, { params: params })
  .pipe(catchError(this._errorHandling.handleError.bind(this)));}

以上命令在我的.net核心API上点击此端点:

    [Authorize(Roles = "Client")]
    [Route("")]
    [HttpPut]
    public IActionResult C_Update(int peopleFieldID, string peopleFieldName )
    {
        //Make call to the proper repo method.
        return Json(_peopleFieldRepo.C_Update(peopleFieldID, peopleFieldName));
    }

两个参数peopleFieldID和peopleFieldName始终为0且为null。我已经确定Angular前端正确地发送了params,但是后端无法识别它们。我还有很多其他端点可以正常工作。

angular asp.net-web-api asp.net-core angular-httpclient
1个回答
3
投票

您需要使用HttpParams设置查询参数,如果没有,则需要将null传递给有效负载:

const params = new HttpParams()
            .set('peopleFieldID', peopleFieldID.toString())
            .set('peopleFieldName', peopleFieldName);
// if you have a payload to pass you can do that in place of null below
return this.http
  .put<number>(this.url, null, { params: params })
  .pipe(catchError(this._errorHandling.handleError.bind(this)));}
© www.soinside.com 2019 - 2024. All rights reserved.