[英]Send IEnumerable from web api controller to angular js
I'm trying to send a IEnumerable from a web api controller to a AngularJs controller. 我正在尝试将IEnumerable从Web api控制器发送到AngularJs控制器。 The code I was using was
我使用的代码是
Web Api: 网络API:
readonly InventoryEntities _db = new InventoryEntities();
public IEnumerable<FDVOEligibilityRequest> Get()
{
return _db.FDVOEligibilityRequests.AsEnumerable();
}
AngularJS: AngularJS:
//get all customer information
$http.get("/api/Customer/").success(function (data) {
$scope.requests = data;
$scope.loading = false;
})
.error(function () {
$scope.error = "An Error has occured while loading posts!";
$scope.loading = false;
});
This worked fine, but now I'm using linq to include related tables and it doesn't work. 这个工作正常,但是现在我使用linq来包含相关表,但是它不起作用。 The angularJs code is the same.
angularJs代码是相同的。 What am I doing wrong?
我究竟做错了什么?
readonly InventoryEntities _db = new InventoryEntities();
public IEnumerable<FDVOEligibilityRequest> Get()
{
return _db.FDVOEligibilityRequests
.Include("FDVOEligibilityRequestMandatoryField")
.Include("FDVOEligibilityRequestDCCField").AsEnumerable();
}
I do get the data I want in the controller, but when i try to send it back to angular, I get a 500 (Internal Server Error) angular.js:10722 我确实在控制器中获取了我想要的数据,但是当我尝试将其发送回angular时,我得到了500(内部服务器错误)angular.js:10722
This is what FDVOEligibilityRequest looks like in the new Web Api controller 这就是FDVOEligibilityRequest在新的Web Api控制器中的样子
public partial class FDVOEligibilityRequest
{
public int Id { get; set; }
public string TestName { get; set; }
public string GroupName { get; set; }
public int MandatoryFieldsID { get; set; }
public int DCCFieldsID { get; set; }
public virtual FDVOEligibilityRequestMandatoryField FDVOEligibilityRequestMandatoryField { get; set; }
public virtual FDVOEligibilityRequestDCCField FDVOEligibilityRequestDCCField { get; set; }
}
If it's 500 and happens after you successfully make a return from your action then it can be an exception during serialization. 如果它是500,并且在您成功从操作返回收益之后发生,那么在序列化期间可能是一个例外。
I would suggest to check that there are no circular references between FDVOEligibilityRequest and FDVOEligibilityRequestMandatoryField/FDVOEligibilityRequestDCCField. 我建议检查一下FDVOEligibilityRequest和FDVOEligibilityRequestMandatoryField / FDVOEligibilityRequestDCCField之间是否没有循环引用。
Or try to diagnose it using code from here : 或者尝试使用此处的代码进行诊断:
string Serialize<T>(MediaTypeFormatter formatter, T value)
{
// Create a dummy HTTP Content.
Stream stream = new MemoryStream();
var content = new StreamContent(stream);
/// Serialize the object.
formatter.WriteToStreamAsync(typeof(T), value, stream, content, null).Wait();
// Read the serialized string.
stream.Position = 0;
return content.ReadAsStringAsync().Result;
}
try {
var val = _db.FDVOEligibilityRequests
.Include("FDVOEligibilityRequestMandatoryField")
.Include("FDVOEligibilityRequestDCCField").AsEnumerable();
var str = Serialize(new JsonMediaTypeFormatter(), val);
}
catch (Exception x)
{ // break point here
}
ps: the common suggestion in this case is to use DTOs instead of EF objects with something like Automapper . ps:在这种情况下, 通常的建议是使用DTO代替带有Automapper之类的EF对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.