title: Web Api and AngularJs
body: <p>Straightly diving into the problem:</p>
  
  <p>There is angularJs controller in which it calls a function defined inside a factory.
  The factory function calls an Api POST action which accepts a '[FromBody]string' parameter.
  The problem is raised exactly here. Actually, the parameter is always null! while inside the factory method it has the desired value. Here is a bit of code:</p>
  
  <p>The angularJs controller:</p>
  
  <pre><code>$scope.readText = function() {
  
          var element = document.getElementsByClassName('news-content');
  
          if (element[0] != undefined) {
              elementPureText = element[0].innerText;
          }
  
          dataFactory.createTextFile(elementPureText)
              .succes(function(data, status, headers, config) {
  
              }).error(function(data, status, headers, config) {
  
              });
      };
  </code></pre>
  
  <p>The factory code:</p>
  
  <pre><code>philadelphiaApp.factory('dataFactory', ['$http', function ($httpt) {
  
  var dataFactory = {};
  
  dataFactory.createTextFile = function (text) {
      return $httpt.post('api/textmanager/textWriter', { '': text });
  };
  
  return dataFactory;
  </code></pre>
  
  <p>}]);</p>
  
  <p>And finally the ApiController:</p>
  
  <pre><code>[HttpPost]
      [ActionName("TextWriter")]
      public HttpResponseMessage PostTextWriter([FromBody]string text)
      {
          if (String.IsNullOrEmpty(text) || text.Length == 0)
          {
              throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest));
          }
  
          if (!Directory.Exists(FileDirectory))
          {
              try
              {
                  Directory.CreateDirectory(FileDirectory);
              }
              catch (Exception)
              {
  
                  throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest));
              }
          }
  
          try
          {
              File.WriteAllText(FileDirectory, text);
          }
          catch (Exception)
          {
              throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest));
          }
  
          return Request.CreateResponse(HttpStatusCode.OK, text);
      }
  </code></pre>
  
  <p>After visiting and searching over the web I came across with a lot of forums and websites which provide solutions but I could not handle it yet. I assume the best out of which is the following URL:</p>
  
  <p><a href="http://encosia.com/using-jquery-to-post-frombody-parameters-to-web-api/" rel="nofollow">http://encosia.com/using-jquery-to-post-frombody-parameters-to-web-api/</a></p>
  
  <p>I highly appreciate any help on this...</p>