【问题标题】:Return HttpResponseMessage in OData Action在 OData 操作中返回 HttpResponseMessage
【发布时间】:2013-09-10 23:12:54
【问题描述】:

我想返回一个匿名类型,为此我使用 HttpResponseMessage,但结果是代码 406 Not Acceptable。这是我的代码

在 WebApiConfig 中

modelBuilder.EntitySet<Groups>("Groups");
var getComplete = modelBuilder.Entity<Groups>().Collection.Action("GetComplete");
getComplete.Returns<HttpResponseMessage>();

在控制器中

[HttpPost]
[Queryable]
public HttpResponseMessage GetComplete(ODataActionParameters parameters)
{
        return this.Request.CreateResponse(HttpStatusCode.OK,db.Groups.Select(c => new 
        {
            ID = c.ID,
            DocumentType= c.DocumentType,
            Name = c.Name ,               
            Debits = c.GroupMvtos.Sum(cm => cm.Debits) ?? 0,
            Credits = c.GroupMvtos.Sum(cm => cm.Credits) ?? 0
        }));            
}

使用 httpget 方法将 ApiController 作为基类可以正常工作,但在 odata 操作中使用 EntitySetController 则不行。

有什么想法吗?

谢谢。

【问题讨论】:

    标签: asp.net-web-api odata


    【解决方案1】:

    OData 操作不能返回匿名对象。操作返回的类型必须声明为服务 EDM 模型。

    【讨论】:

    • 那么我怎样才能像在 ApiController 中一样返回匿名对象的集合
    • OData 集合是同构且强类型的。因此,V3 协议中没有办法返回匿名对象。你不能声明一个类型并使用它来代替匿名类型吗?
    • 我不能,因为我需要 Queryable 能力。我想在 IQueryable 中使用 $filter、$top 等,并且只调用一次数据库,并且不需要额外的步骤来将结果解析为强类型。我想要 ApiController 中的 httpget 方法,您可以在其中执行此操作。
    • 另一个需要改进的答案
    猜你喜欢
    • 2013-02-23
    • 2013-06-13
    • 2012-09-19
    • 2023-03-08
    • 2014-11-22
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 2016-07-02
    相关资源
    最近更新 更多