【问题标题】:Send IEnumerable from web api controller to angular js将 IEnumerable 从 web api 控制器发送到 angular js
【发布时间】:2016-09-13 12:24:49
【问题描述】:

我正在尝试将 IEnumerable 从 Web api 控制器发送到 AngularJs 控制器。 我使用的代码是

网页接口:

readonly InventoryEntities _db = new InventoryEntities();

public IEnumerable<FDVOEligibilityRequest> Get()
{
   return _db.FDVOEligibilityRequests.AsEnumerable();
}

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;
    });

这很好用,但现在我使用 linq 来包含相关表,但它不起作用。 angularJs 代码是一样的。 我做错了什么?

readonly InventoryEntities _db = new InventoryEntities();

public IEnumerable<FDVOEligibilityRequest> Get()
{
    return _db.FDVOEligibilityRequests
        .Include("FDVOEligibilityRequestMandatoryField")
        .Include("FDVOEligibilityRequestDCCField").AsEnumerable();
}

我确实在控制器中获得了我想要的数据,但是当我尝试将其发送回 angular 时,我得到一个 500(内部服务器错误)angular.js:10722

这就是 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; }
}

【问题讨论】:

  • 不起作用是什么意思?你得到的是什么而不是预期的结果?
  • 我在环侧收到错误
  • 您的新旧 Web Api 控制器的返回类型是相同的,但您是否在新的控制器中添加了属性?我要问的是,您的模型 FDVOEligibilityRequest 仍然是包含语句的有效模型吗?
  • 这是 FDVOEligibilityRequest 在新的 Web Api 控制器中的样子
  • 公共部分类 FDVOEligibilityRequest { public int Id { get;放; } 公共字符串 TestName { 获取;放; } 公共字符串组名 { 获取;放; } 公共 int MandatoryFieldsID { 获取;放; } 公共 int DCCFieldsID { 获取;放; } 公共虚拟 FDVOEligibilityRequestMandatoryField FDVOEligibilityRequestMandatoryField { 获取;放; } 公共虚拟 FDVOEligibilityRequestDCCField FDVOEligibilityRequestDCCField { 获取;放; } }

标签: angularjs linq http asp.net-web-api get


【解决方案1】:

如果它是 500 并且在您成功地从您的操作返回之后发生,那么它可能是序列化期间的异常。

我建议检查 FDVOEligibilityRequest 和 FDVOEligibilityRequestMandatoryField/FDVOEligibilityRequestDCCField 之间是否存在循环引用。

或者尝试使用来自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:在这种情况下,common suggestion 是使用 DTO 而不是 EF 对象,例如 Automapper

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-16
    • 2015-05-03
    • 2021-10-12
    • 2020-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-07
    相关资源
    最近更新 更多