Terrance Jackson
Terrance Jackson

Reputation: 1079

How to pass a list of string to WebAPI get method/

My call from angular

 private apiResource = environment.baseUrl + 'api/Resource'; 

    getQueryUserPresence(userEmails: any[]): Observable<any> {
        let params =  new HttpParams().set('userEmails', userEmails);
        return this.http.get<any>(this.apiResource, {params: params });   
      }

My method in WebAPI. The userEmailsis a list however it is always null. for some reason. If I pass a single string and change it to just one string then it works. When I change to a list it is null

 public Team Get(List<string> userEmails)
        {   }

Upvotes: 1

Views: 1365

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222712

Try something like below,

let params = new HttpParams();
params = params.append('userEmails', 'test1');
params = params.append('userEmails', 'test2');
params = params.append('userEmails', 'test3');
return this.http.get<any>(this.apiResource, {params: params });  

one more change you need on the Web API is to use FormQuery,

public Team  Get([FromQuery] List<string> userEmails)

Upvotes: 1

Related Questions