Straightly diving into the problem:
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:
The angularJs controller:
$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) {
});
};
The factory code:
philadelphiaApp.factory('dataFactory', ['$http', function ($httpt) {
var dataFactory = {};
dataFactory.createTextFile = function (text) {
return $httpt.post('api/textmanager/textWriter', { '': text });
};
return dataFactory;
}]);
And finally the ApiController:
[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);
}
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:
http://encosia.com/using-jquery-to-post-frombody-parameters-to-web-api/
I highly appreciate any help on this...