title: Angular Posting to .net Web API
body: <p>I am trying to execute a POST from my angular application to a .net Web API instance but the server returns <code>null</code>.</p>
  
  <p><strong>server</strong> </p>
  
  <pre><code>    [HttpPost] 
      public string callBcknd([FromBody]string body)
      {
          try
          {
              Log.Info(string.Format("{0}", body));
  
          }
          catch(Exception ex)
          {
              return "error";
          }
      }
  }
  </code></pre>
  
  <p><strong>angular</strong> *note that I am using HttpClient instead of Http.. not sure if this is also the problem</p>
  
  <pre><code>callServer(){
      var test = { "name": "John" }
      let data = JSON.stringify(test);
      let headers = new HttpHeaders(); 
      headers.set('Content-Type', 'application/json');
      this.appService.http.post('http://localhost:3000/api/WebApI/callBcknd', 
                            test, 
                            {headers: headers})
    .subscribe(data =&gt; {console.log(data);}}}
  </code></pre>
  
  <p><strong>config</strong></p>
  
  <pre><code>public static class WebApiConfig
      {
          public static void Register(HttpConfiguration config)
          {
  
              config.MapHttpAttributeRoutes();
  
              config.Routes.MapHttpRoute(
                  name: "DefaultApi",
                  routeTemplate: "api/{controller}/{action}/{id}",
                  defaults: new {action = "GET", id = RouteParameter.Optional}
              );
          }
      }
  </code></pre>
  
  <p>With above setting, I don't gen any 404 server error in client side(by checking chrome's console) but it returns null in backend. But as I tried with Postman, it sends values properly with the same url. If I don't include [FromBody] inside of method in backend, I get an 404 server error in client side. Furthermore, messages says "NO HTTP resource was found that matches the request URI". Similar question to this seems solving the problem by having [FromBody] but I still get a null... I also suspected maybe my web config file(not the one in above) should contain some headers, so when I added some headers like content type to be json and etc then I get 500 server error in client side. At this point i am really confused and not sure what to proceed. </p>
  
  <p><strong>UPDATE1</strong></p>
  
  <p>Following server code returns the message but I am still getting the body as null.. no errors have been observed  </p>
  
  <pre><code>[HttpPost]
          public IHttpActionResult Callbcknd([FromBody] string body)
          {
              try
              {
                  Log.Info(string.Format("called with data {0}", body));
  
                  return Ok(new { Message = "It worked!" });
              }
  
              catch(Exception ex)
              {
                  return base.Content(HttpStatusCode.InternalServerError, ex.ToString());
              }
          }
  </code></pre>