Search code examples
c#asp.net-mvcasp.net-web-apiasp.net-web-api-odata

ASP.NET WebAPI OData Delta object fields is null in PATCH request


I have created ASP.NET WebAPI with OData support.

The Project class has a field named ProjectType with nullable integer type declaration:

public class Project
{
        public string Id { get; set; }
        public string Name { get; set; }
        public int? ProjectType { get; set; }
}

My WebAPI Controller:

public class LocalModelsController : ODataController
{
  public IHttpActionResult Patch([FromODataUri] string key, Delta<Project> patch)
  {   
        var projectUpdated = patch.GetEntity();
        var projectType = projectUpdated.ProjectType;

        //projectType is null here
  }
}

When I send the PATCH http request (with BSON content type format) from client to WebAPI, the Project object has correct values of Id and Name, but ProjectType is always null.

But when I change the type of ProjectType field type from int? to double?, it works correct:

public double? ProjectType { get; set; }

Is it a limitation of ODATA or I were missing something?


Solution

  • I have worked it out. It is just because I was using BSON format to transfer objects throught HTTP request. It works well if I use JSON instead. Not sure what wrong with BSON format though.