aqbhectic
aqbhectic

Reputation: 53

Implementing PATCH in webapi

I have an object that I want to update partially using webapi/json here is an example of my model

   public class Location
    {
        public int Id { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string PostalCode { get; set; }
    }

The JSON from the client will be

 {
    "Id": 1, 
    "Address":"new address" 
  }

The webapi function looks like this

    public bool Patch(Location location)
    {
        //do something
    }

Problem is the only field updated was the address so without checking each field for string.isnullorempty I can't tell what has changed and more over null/empty could just mean delete the value is there a more seamless way to do this?

Upvotes: 4

Views: 3729

Answers (2)

Lukas S.
Lukas S.

Reputation: 5758

JSON Patch is not natively supported by ASP.NET Web API. There are currently two implementations for the JSON-patch spec that are available for .NET (at least that I'm aware of):

myquay/JsonPatch
Github: https://github.com/myquay/JsonPatch
NuGet: https://www.nuget.org/packages/JsonPatch/1.0.0

KevinDockx/JsonPatch
GitHub: https://github.com/KevinDockx/JsonPatch
NuGet: https://www.nuget.org/packages/Marvin.JsonPatch/0.3.0

Both of these are currently in "alpha" status, and neither of them implement the spec fully yet.

Upvotes: 2

Darrel Miller
Darrel Miller

Reputation: 142044

Not really. That's why there is Json-patch however, to my knowledge no-one has written a .net library for it.

Upvotes: 1

Related Questions