0

I am working on project in which I want to add a value to an Oracle database and get value from it. I am using Angular 4 using VSCode and ASP.NET Web API using Visual Studio 2015 for services.

My Angular code is working well but it's not hitting the required method in the ASP.NET Web API while calling.

My Angular service code is

register(userInfo: any) {
    debugger;        
    var url1 = this.baseUrl;
    url1 += "api/signUp/SaveCustomer";
     return this._http.post(url1,userInfo);
  };

here base url is BASE_API_URL: "http://localhost:6705/.

My ASP.NET Web API service method is

[HttpPost]
public IHttpActionResult SaveCustomer( UserInfo user )
{
    // int result;

    // result = _customerManager.SaveCustomer(user);
    // if (result > 0)
    // {
    //    return Ok(result);
    // }
    // else
    // {
    //    return NotFound();
    // }
    return null;
}

and its controller constructor is

public signUpController()
{
    _customerManager = new CustomerManager();
}

When I run the project, I have put debugger at two places one at constructor and other at method Register. My control comes to constructor but its not hit the required method. I am really worried I am new to this technology. No error is showing on screen.

2
  • check the browser console for error Jan 6, 2018 at 7:37
  • Angular is running in the development mode. Call enableProdMode() to enable the production mode. is written on there no error is display Jan 8, 2018 at 7:25

2 Answers 2

0

Use [FromBody] in action like this:

[HttpPost]
public IHttpActionResult SaveCustomer([FromBody] UserInfo user)
{
    ...
}
4
  • Either there is any configuration for this?? Jan 6, 2018 at 7:32
  • when i debug from vs code in angular service my path become like this url1 = "localhost:6705/api/signUp/SaveCustomer" Jan 6, 2018 at 7:35
  • Please make sure, all the fields of UserInfo on client side and server side are same. Although, FromBody is required for HTTP post type requests. Jan 6, 2018 at 7:48
  • All are same no sonsole error i am checking sir its sharing control to constructor of this class but unfortunatally its not giving control to method of that class whom i want to run Jan 8, 2018 at 7:31
0

If you call Web API post method from Angular 2 type script, don't forget to add following header content and parameter object.

register(userInfo: any) {
    var params = new URLSearchParams();
    params.set('username', userInfo.username);
    params.set('password', userInfo.password);
    let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
    var url1 = this.baseUrl;
    url1 += "api/signUp/SaveCustomer";

    return this._http.post(url1, params.toString(), { headers: headers })

  };

Update

register(userInfo: any) {

        //let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
        let headers = new Headers({ 'Content-Type': 'application/json' });

       var url1 = this.baseUrl;
        url1 += "api/signUp";
        console.log(url1);
        return this._http.post(url1, userInfo, { headers: headers });

    };
34
  • make sure , FromBody is added in your Post method Jan 8, 2018 at 7:17
  • public IHttpActionResult SaveCustomer([FromBody] UserInfo user ) is added sir and sir main issue is that no error is showing Jan 8, 2018 at 9:37
  • Its working with constructor but not getting the register method Jan 8, 2018 at 9:37
  • Let me reproduce this issue at my end Jan 8, 2018 at 9:48
  • How can i reproduce it sir? Jan 8, 2018 at 11:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.