'HTTP Patch Delta is Null with oData web service

I am using entity framework code first. My entities and controller were generated by Visual Studio and have not been modified.

This issue seems to be that the DELTA for my patch request is null. The innererror on the web service is 'Object reference not set to an instance of an object' which is thrown on the Validate() function.

Here is my web service - this is a PUT service that behaves like a PATCH. This is to work around a bug with the Agentry framework which doesn't seem to work with Patch.

// PUT: odata/Device(5)
    public IHttpActionResult Put([FromODataUri] long key, Delta<LineSeg> patch)
    {
        Validate(patch.GetEntity());

        if (!ModelState.IsValid)
        {
            return NotFound();
        }

        LineSeg LineSeg = db.LineSeg.Find(key);
        if (LineSeg == null)
        {
            return NotFound();
        }

        //note - put is acting as a patch due to agentry bug
        patch.Patch(LineSeg);

        try
        {
            db.SaveChanges();
        }
        catch (DbUpdateConcurrencyException)
        {
            if (!LineSegExists(key))
            {
                return NotFound();
            }
            else
            {
                throw;
            }
        }

        return Updated(LineSeg);
    }

Here is my request:

Header:

Content-Type: application/atom+xml

Body:

<?xml version="1.0" encoding="utf-8"?>
<entry xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<content type="application/xml">
  <m:properties>
     <d:RESULTS_ID m:type="Edm.Int64">399</d:RESULTS_ID>
  </m:properties>



Solution 1:[1]

you miss [FromBody]

public IHttpActionResult Put([FromODataUri] long key, [FromBody] Delta<LineSeg> patch)
...

Is required for the controller desealize the object

Solution 2:[2]

In my case problem was caused by Nswag package. I had to get rid of it completely and problem was resolved. And also I had to remove [FromODataUri] and [FromBody]. so final result should be :

public IHttpActionResult Put(long key, Delta<LineSeg> patch)

Also take a look at samples provided here: https://github.com/OData/AspNetCoreOData Had to upgrade to latest nuget packages for odata and other packages there for samples to work.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 FRL
Solution 2 user1892777