Search code examples
asp.netangularangular-httpclient

Angular 7 send two parameters or more to ASP.NET server


I'm new to Angular, I'm using Angular 7 as client side and ASP.NET as server side.

I want to pass more than 2 parameters. I've succeeded to send one parameter.

Here's my Angular service method that sends 2 parameters:

validate_user(user : Users, active: boolean): Observable<UsersError> {
    console.log(user);

    return this.http.post<UsersError>(this.users, user, active);
}

Here's my ASP.NET post method:

public UsersError Post([FromBody] sp_GetUsers_Result user, bool active)
{
        UsersError u = new UsersError
        {
            UserName = "Username Invalid",
            Password = "Password Invalid"
        };

        return u;
}

I know I can wrap the parameters into an object and send it as one, but I want to know is there a way to do it with two or more parameters (objects).

I tried to use HttpParams with various combinations, but nothing worked.


Solution

  • The third parameter in .post is httpOptions :

    Example:

    const httpOptions = {
      headers: new HttpHeaders({
        'Content-Type':  'application/json',
        'Authorization': 'my-auth-token'
      })
    };
    

    So you'd need to change this line :

    return this.http.post<UsersError>(this.users, user, active);
    

    active should be send as param

    Regarding asp.net : submitting/posting must match an object in the controller.

    Asp.net wil take the body params and will try to match them into the controller argument (fromBody). You can't mix it with another argument(*).

    In short , you'd have to add active to a new model in server.

    * You can , but it will require some other hacks - like I've built: https://github.com/RoyiNamir/SimplePostVariableParameterBindingExtended