Web Api和AngularJs
直接切入问题: 有一个angularJs控制器,它在其中调用工厂内定义的函数。 工厂函数调用接受“[FromBody]string”参数的Api POST操作。 问题就在这里提出。实际上,参数总是空的!而在工厂方法中,它具有所需的值。下面是一些代码: angularJs控制器:Web Api和AngularJs,angularjs,asp.net-mvc-4,asp.net-web-api,Angularjs,Asp.net Mvc 4,Asp.net Web Api,直接切入问题: 有一个angularJs控制器,它在其中调用工厂内定义的函数。 工厂函数调用接受“[FromBody]string”参数的Api POST操作。 问题就在这里提出。实际上,参数总是空的!而在工厂方法中,它具有所需的值。下面是一些代码: angularJs控制器: $scope.readText = function() { var element = document.getElementsByClassName('news-content');
$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) {
});
};
[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);
}
工厂代码:
philadelphiaApp.factory('dataFactory', ['$http', function ($httpt) {
var dataFactory = {};
dataFactory.createTextFile = function (text) {
return $httpt.post('api/textmanager/textWriter', { '': text });
};
return dataFactory;
}]))
最后是ApicController:
$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) {
});
};
[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:
非常感谢您在这方面提供的任何帮助…您正在通过一个名为“”的字段(空字符串)和文本值的对象发送。但是,webapi后端需要的是字符串,而不是对象。它无法将对象反序列化为字符串,因此它将为null 您需要直接发送字符串,而不是将其填充到对象中。像这样:
return $httpt.post('api/textmanager/textWriter', text);
问题在于数据是如何编码的,以及WebAPI是如何试图在后端反序列化数据的 默认情况下,
$http
将通过内容类型为application/json
的所有POST请求,并尝试以这种方式对数据进行编码。您可以尝试将内容类型
设置为类似于文本/普通
的内容,但是您需要一个自定义媒体格式化程序,以便WebAPI能够处理该问题
这里阻力最小的方法是创建一个简单的请求对象,如下所示:
public class TextWriterRequest
{
public String Text { get; set; }
}
然后修改方法以接受该对象作为输入。无需使用[FromBody]
,因为默认情况下WebAPI将在body中查找复杂对象
public HttpResponseMessage PostTextWriter(TextWriterRequest text)
然后,您可以通过$http
服务确保所传递的数据与请求对象匹配:
$http({
method: 'POST',
url: '/api/textmanager',
data: { text: text }
});
这将在服务中正确地反序列化,生活将再次美好。事实上,经过几天的努力,我找到了答案。。。这似乎很容易;只需在web api客户端调用中添加内容类型:application/json,做得很好!
但是感谢那些提供解决方案的人。谢谢,我以前做过,但仍然是空值。事实上,我按照上面提到的URL(在我的问题中)的建议发送了对象。。你的建议似乎是一个合理的解决方案,但我很抱歉地说,不,它在我的代码中不起作用。。。因为我正在开发一个示例应用程序,而且完全出乎意料。。我会从头开始创建一个新的解决方案,也许我能找到它@这对我来说似乎很奇怪。主要是因为我在回答这个问题之前创建了一个解决方案,以确保它按预期工作。