Return HttpResponseMessage in OData Action

Topics: ASP.NET Web API
Sep 10, 2013 at 5:49 AM
Hi,

I want return an anonimoustype and for that i use HttpResponseMessage but the result is the code 406 Not Acceptable. This is my code

In WebApiConfig
modelBuilder.EntitySet<Groups>("Groups");
var getComplete = modelBuilder.Entity<Groups>().Collection.Action("GetComplete");
getComplete.Returns<HttpResponseMessage>();
In controller
[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
            }));            
}
Any Ideas?

Thanks
Feb 9, 2014 at 11:12 AM
Edited Feb 9, 2014 at 11:12 AM
Had a similar issue:
builder.Entity<MyEntity>()
        .Action("DoMyAction")
        .ReturnsFromEntitySet<MyReturnType>("MyReturnType");
[HttpPost]
[Queryable]
public IHttpActionResult DoMyAction([FromODataUri] int key, ODataActionParameters parameters)
{
    MyBaseType obj = GetMyObject();

    return Ok(obj); // <-- obj is 'MyReturnType' at runtime. But this code only knows the base type 'MyBaseType' at compile time.
}
The code above return a HTTP 406 when called.
Changing the code above to the code below fixed the issue. (Added an explicit cast to the correct type in the Ok() call:
[HttpPost]
[Queryable]
public IHttpActionResult DoMyAction([FromODataUri] int key, ODataActionParameters parameters)
{
    MyBaseType obj = GetMyObject();

    return Ok((MyReturnType) obj); // <-- obj is 'MyReturnType' at runtime. But this code only knows the base type 'MyBaseType' at compile time.
}