Search code examples
dynamicentityodataasp.net-web-apiasp.net-web-api2

Typless Entity Patch operation in ODATA v4


I'm workng on supporting the patch operation for a typeless entity in odata v4 with webapi.

So far the only thing that I was able to make work was the Put operation, but it requires the whole object to be present.

public HttpResponseMessage Put(IEdmEntityObject entity)
{
     return Request.CreateResponse(System.Net.HttpStatusCode.NoContent);
}

Whenever I change the Put verb to be a Patch, the entity object is null.

public HttpResponseMessage Patch(IEdmEntityObject entity)
{
     return Request.CreateResponse(System.Net.HttpStatusCode.NoContent);
}

I also tried replaceing the IEdmEntityObject with the types - Delta, Delta<IEdmEntityObject> and dynamic but i get the following exception

No MediaTypeFormatter is available to read an object of type 'Object' from content with media type 'application/json'

Solution

  • gebov

    Update, for example PATCH/PUT is for a single entity. So, your action should have a key to identify which entity should to be updated.

    Therefore, modify your action as:

    public HttpResponseMessage Patch(int key, IEdmEntityObject entity)
    {
       ...
    }
    

    It should work. See my test project. Thanks.