I've a class like this in angular
export class UserSearch {
firstName: string;
lastName: string;
city: string;
stateCode: string;
}
I wanted to send an instance of UserSearch with data to web api from angular. I've used below 2 different ways but still no luck. I can always see null at web api. Code in angular service:
getSearchResults() {
var request: UserSearch = {} as any;
request.firstName='fName';
request.lastName='lName';
request.city='city1';
request.stateCode='sCode';
var params = new HttpParams().append('searchRequest', JSON.stringify(request));
return this.http.get('https://localhost:44348/api/userSearchResults/search', params);
}
Api is receiving null if i use the above procedure. So, I've tried like following:
getSearchResults() {
var request: UserSearch = {} as any;
request.firstName='fName';
request.lastName='lName';
request.city='city1';
request.stateCode='sCode';
return this.http.get(`https://localhost:44348/api/userSearchResults/search?searchRequest=${request}`);
}
But still no luck, web api action method still receiving null.
Here is my web api action method:
[HttpGet, Route("search")]
public IActionResult Search(UserSearch searchRequest) {
return Ok(_service.GetSearchResults(searchRequest));
}
UserSearch class in C# web api looks like following:
public class UserSearch {
public string firstName {get; set;}
public string lastName {get; set;}
public string city {get; set;}
public string stateCode {get; set;}
}
What is the best way to send class instance with data to web api controller from angular? Can someone please go through the code and let me where am I doing wrong? Thanks in Advance.