Skip to content

J0rgeSerran0/JSONPatch-ASP.NET-Core_5-Web-API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

JSON Patch in an ASP.NET Core 5 Web API

Demo to show you as use JSON Patch in an ASP.NET Core 5 Web API

.NET 5

This sample code use .NET 5 and C# as programming language.

You will find the last .NET 5 SDK version here

NuGet Packages needed

Note that there is another package that is used here, Microsoft.AspNetCore.JsonPatch, however, this package is a reference of Microsoft.AspNetCore.Mvc.NewtonsoftJson.

Testing the Web API

Start the Web API and execute the next operations:

Create new Employee

POST operation to the endpoint https://localhost:44372/api/employee/

JSON payload in the Body

{
    "Name": "Charles",
    "Age": 22
}

The response will be a 200 OK with a Guid value that will be the id of the employee

Get an Employee by Id

GET operation to the endpoint https://localhost:44372/api/employee/{id} where {id} is the Guid created in the POST operation

You will receive a 200 OK with a JSON information like:

{
    "id": "93a5f5e3-dfcf-40dc-9d85-2d0757302d52",
    "name": "Charles",
    "age": 22
}

Get all the employees

GET operation to the endpoint https://localhost:44372/api/employee/

You will receive a 200 OK with all the employees, with a JSON information like:

[
    {
        "id": "93a5f5e3-dfcf-40dc-9d85-2d0757302d52",
        "name": "Frank",
        "age": 37
    }
]

Update some data of an existing employee

PATCH operation to the endpoint https://localhost:44372/api/employee/update/{id} where {id} is the Guid created in the POST operation

To call to this operation, you will have to send a payload with an arry with each operation that you want to do.

If you want to modify the Name and Age of an employee, you will have to send two operations in the payload like:

[
    {"op" : "replace", "path" : "/Name", "value" : "Frank"},
    {"op" : "replace", "path" : "/Age", "value" : "37"}
]

You will receive a 200 OK with the employee modified:

{
    "id": "93a5f5e3-dfcf-40dc-9d85-2d0757302d52",
    "name": "Frank",
    "age": 37
}

References

RFC-6902

Releases

No releases published

Packages

No packages published

Languages