在HttpParams中删除null参数

问题描述 投票:3回答:2

我有一个标准对象,其中一些属性可以为null,如果我不做任何事情,查询字符串包括那些像&p=undefined这是不可取的,因为在WebApi这些来自as"null"而不是null所以在Angular客户端给出这个

      return this._http
          .get<ExposureDifference[]>(AppSettings.ExposureRunDataQueryString, {params : <any>criteria, withCredentials: true}) 
          .catch<any, ExposureDifference[]>(this._trace.handleError("GET " + AppSettings.ExposureRunDataQueryString + criteria, []))
          .finally(() => this.isLoading = false);

我可以将查询字符串作为

http://localhost:63037/api/exposureRuns/data/?id=3&secsymb=undefined&fund=EL&esectype=SWAP_CDS

有没有办法从查询字符串中排除未定义的参数?

angular query-string asp.net-core-2.0 asp.net-core-webapi querystringparameter
2个回答
1
投票

你可以试试这个:

this.httpClient.get('../api/path', { 
  params: Object.entries(queryParameterObject).reduce((queryParams, [key, value]) => queryParams.set(key, value), new HttpParams());
});

它对我有用。

© www.soinside.com 2019 - 2024. All rights reserved.