【发布时间】:2014-08-03 05:28:15
【问题描述】:
直奔问题:
有一个 angularJs 控制器,它调用工厂内部定义的函数。 工厂函数调用接受“[FromBody]string”参数的 Api POST 操作。 问题正是在这里提出的。实际上,参数始终为空!而在工厂方法内部,它具有所需的值。下面是一段代码:
angularJs 控制器:
$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) {
});
};
工厂代码:
philadelphiaApp.factory('dataFactory', ['$http', function ($httpt) {
var dataFactory = {};
dataFactory.createTextFile = function (text) {
return $httpt.post('api/textmanager/textWriter', { '': text });
};
return dataFactory;
}]);
最后是 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);
}
通过网络访问和搜索后,我遇到了很多提供解决方案的论坛和网站,但我还无法处理。我认为最好的是以下 URL:
http://encosia.com/using-jquery-to-post-frombody-parameters-to-web-api/
非常感谢您对此提供的任何帮助...
【问题讨论】:
标签: angularjs asp.net-mvc-4 asp.net-web-api