Search code examples
asp.net-web-api2odatafrombodyattribute

OData V4 function FromBody parameter


This OData function does not deserialize the model parameter from the body. It deserializes as null as seen from response. Is there support for FromBody parameters in OData V4?

ConfigV1.cs

builder.Function("CreateTestModel").Returns<TestModel>();
var edmModel = builder.GetEdmModel()
config.MapODataServiceRoute("ODataRouteV1", "v1", edmModel);

TestController.cs

using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.OData;
using System.Web.OData.Query;
using System.Web.OData.Routing;

public class TestController : ODataController
    [HttpPost]
    [ODataRoute("CreateTestModel")]
    public TestModel CreateTestModel([FromBody]TestModel model)
    {
        return model;
    }
}

TestModel.cs

public class TestModel
{
    public string Value { get; set; }
}

Request

POST /v1/CreateTestModel HTTP/1.1
Host: localhost:8090
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: 4810cdc0-d92b-b7b5-4328-8b87e0222854

{
    "Value": "test"
}

Response

{
  "@odata.context":"http://localhost:8090/V1/$metadata#Edm.Null","@odata.null":true
}

Solution

  • OData Functions should be called with an HTTP GET and shouldn't affect the server. Your method here CreateTestModel sounds like it will affect the server so I would say that it is probably more suited to an OData Action. This may seem like it isn't relevant but I think that it will actually fix your issue as well because Actions are setup to have parameters in the body whereas Functions typically get parameters from the URL