0

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.

3
  • you need to set headers and options as below : var headers = new HttpHeaders(); headers.append('Content-Type', undefined); const httpOptions = { headers: headers, reportProgress: true }; Commented Sep 8, 2020 at 20:08
  • Hi Guys, I personally feel that modelbinder concept is not a good idea as we need to have an entry in startup.cs file for each and every viewmodel that we are going to create in our application. Which is not a good idea. Anyway I found the easy solution for this: The only chage that I made at angular service which you can see in my next comment as no space in this comment box to leave my code snippet. Commented Sep 9, 2020 at 17:47
  • Here is my solution: getSearchResults(request: UserSearch) { var params = new HttpParams().set('firstName', request.firstName).set('lastName',request.lastName) .set('city',request.city).set('stateCode',request.stateCode); return this.http.get('localhost:44348/api/userSearchResults/search', params); } with this I can see values at web api action nmethod. Commented Sep 9, 2020 at 17:53

1 Answer 1

0

try to use [FromUri], like this:

public IActionResult Search([FromUri] UserSearch searchRequest)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.