英文:
optional parameter in query string
问题
我想使查询字符串中的fio参数成为可选项,如果为空则不发送。查询字符串将如下所示:
http://localhost:5025/EventLog/FromPeriod?DateIn=2023-02-08T11%3A50%3A10.198Z&DateOut=2023-02-08T11%3A50%3A10.198Z&idPunkts=18
我尝试在我的服务方法中添加?fio: string
,但出现了类型错误。我还尝试在API控制器中添加null,但仍然不起作用。
请注意,将fio参数设置为可选项需要对您的代码进行一些更改。您可以尝试以下方法:
在服务方法中将fio参数更改为可选的,并检查其是否存在,然后相应地构建参数:
updateFromPeriod(DateIn: any, DateOut: any, idPunkts: any, fio?: string) {
let params = new HttpParams()
.append('DateIn', this.toISOLocal(DateIn))
.append('DateOut', this.toISOLocal(DateOut))
.appendAll({ 'idPunkts': idPunkts });
if (fio) {
params = params.append('fio', fio);
}
const options = {
params,
headers: new HttpHeaders()
.set('Content-Type', 'application/json')
.set('Authorization', `Bearer ${this.authService.currentSessionValue.token}`)
};
console.log('PARAMS: ' + options.params);
return this.httpClient.get<EventLog[]>(this.BaseUrl + 'EventLog/FromPeriod', options);
}
这样,如果fio参数存在,它将包含在查询字符串中,如果不存在,它将被忽略。
然后,在您的API控制器中,不需要任何其他更改,因为你已经正确处理了可选的fio参数。
英文:
i have the text field 'fio' in my search form, which can be empty.
home.html:
<dx-text-box
[(value)]="fio"
></dx-text-box>
this is my component and service to send the search parametres for API
component:
public fio!: string;
public updateFromPeriod(): void {
this.updateService.updateFromPeriod(this.DateIn, this.DateOut, this.punktsID(this.punktValue), this.fio).subscribe({
next: value => {
this.EventLogs = value;
},
error: err => {
console.error(err);
}
});
service:
updateFromPeriod(DateIn: any,
DateOut: any,
idPunkts: any,
fio: string) {
const options = {
params: new HttpParams().append('DateIn', this.toISOLocal(DateIn)).append('DateOut', this.toISOLocal(DateOut)).appendAll({'idPunkts': idPunkts})
.append('fio', fio),
headers: new HttpHeaders().set('Content-Type', 'application/json').set('Authorization', `Bearer ${this.authService.currentSessionValue.token}`)
};
console.log('PARAMS: ' + options.params);
return this.httpClient.get<EventLog[]>(this.BaseUrl +'EventLog/FromPeriod', options);
}
and server method:
public IEnumerable<EventLog> Get(
[FromQuery] DateTime DateIn,
[FromQuery] DateTime DateOut,
[FromQuery] int[] idPunkts,
[FromQuery] string fio) {
List<int> punkts = idPunkts.ToList();
var login = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;
return EventLogRepository.GetEventLog(DateIn, DateOut, punkts, fio, login);
}
I want to make the fio parameter optional in query string, which will not send, if it's empty. And the query string will be like this
http://localhost:5025/EventLog/FromPeriod?DateIn=2023-02-08T11%3A50%3A10.198Z&DateOut=2023-02-08T11%3A50%3A10.198Z&idPunkts=18
i try to add ?fio: string in my service method, but have the type mistake. Als i tried to add null on api controller, but it still doestn work.
答案1
得分: 0
你应该根据条件将它附加到你的参数中:
let params: HttpParams = new HttpParams().append('DateIn', this.toISOLocal(DateIn)).append('DateOut', this.toISOLocal(DateOut)).appendAll({'idPunkts': idPunkts});
if (fio) { params = params.append('fio', fio); }
const options = {
params: params,
headers: new HttpHeaders().set('Content-Type', 'application/json').set('Authorization', `Bearer ${this.authService.currentSessionValue.token}`)
};
可能有更优雅的方法,但这是基本原则。
英文:
you should conditionally append it to your params:
let params:HttpParams = new HttpParams().append('DateIn', this.toISOLocal(DateIn)).append('DateOut', this.toISOLocal(DateOut)).appendAll({'idPunkts': idPunkts});
if(fio){ params = params.append('fio', fio);}
const options = {
params: params,
headers: new HttpHeaders().set('Content-Type', 'application/json').set('Authorization', `Bearer ${this.authService.currentSessionValue.token}`)
};
There is probably more elegant way but that's the basic principle.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论