Search code examples
.netangularasp.net-mvcasp.net-core

How to pass parameter value when request to server


return this.http.post<User>(`${environment.apiUrl}/api/users/LoginUser/`, { UserName, Password })

I try to pass parameters to the link but parameter values remain null when requests go to the server.


Solution

  •     login(userName:string, password:string) {
            const headers = new HttpHeaders().set('Content-Type', 'application/json');
            var body = {
                "UserName": userName,
                "Password": password
            }
            return this.http.post<User>(`${environment.apiUrl}/api/users/LoginUser/`, body
            ,{
                withCredentials: null,
                headers: headers,
            })
        }
    
    .net
    
        [HttpPost]
            [Route("LoginUser")]
            //[EnableCors("AllowOrigin")]
            public ResponseObject<User> LoginUser(User user)
            {
                UsersBLL bll = new UsersBLL();
        
                User user = bll.LoginUser(user.UserName, user.Password);
        
                string Token = Common.JwtHelper.CreateToken(user.Id.ToString(), user.Email);
        
                user.Token = Token;
        
                bll.UpdateUser(user);
        
                return SvcResponse.SuccessReponse<User>(user);
        
            }