4

I'm utilizing KevinDockx's JsonPatch extension for Visual Studio. My project uses .NET Web API (not Core) and Angular (6). .Net Web API doesn't support the JsonPatchDocument namespace, thus the need for KevinDock's JsonPatch extension.

I'm reading through his documentation here: https://github.com/KevinDockx/JsonPatch/blob/master/README.md

JsonPatch consists of two parts:

On the client (consumer of the API): the JsonPatchDocument / JsonPatchDocument class to build what's essentially a change set to be applied to your object on your API side.

At (Web) API level: an ApplyTo method to apply those changes to your objects.

But I'm having trouble understanding part 1, where he builds a JsonPatchDocument class on the client. In my case, I assume that means I need to build this JsonPatchDocument somewhere in the Angular. The code for this step is:

JsonPatchDocument<DTO.Expense> patchDoc = new JsonPatchDocument<DTO.Expense>();
patchDoc.Replace(e => e.Description, expense.Description);

// serialize
var serializedItemToUpdate = JsonConvert.SerializeObject(patchDoc);

// create the patch request
var method = new HttpMethod("PATCH");
var request = new HttpRequestMessage(method, "api/expenses/" + id)
{
    Content = new StringContent(serializedItemToUpdate,
    System.Text.Encoding.Unicode, "application/json")
};

// send it, using an HttpClient instance
client.SendAsync(request);

However, I'm unsure of how to implement this portion of code in my Angular code, or if I'm even interpreting that part of the instructions correctly. For example, say I've already installed JsonPatch on my API via the NuGet package manager console - how then could I reference JsonPatchDocument from Angular (JsonPatchDocument<DTO.Expense> patchDoc = new JsonPatchDocument<DTO.Expense>();), since Angular does not have this install? Has anyone worked with JsonPatch in an Angular project enough to interpret this part of the documentation for me?

3
  • Hey Mate, you ever found a solution for this or just used the [FormBody] approach? Commented Aug 18, 2020 at 17:35
  • Hi @Dash, unfortunately, JsonPatch proved too complex with not enough documentation for our needs. We ended up going with standard PUT and POST requests and abandoning PATCH requests altogether. Commented Aug 18, 2020 at 22:41
  • 1
    Thanks for the update. I'm currently implementing similar to your need and ended doing something mentioned in the following link. c-sharpcorner.com/UploadFile/97fc7a/… Commented Aug 19, 2020 at 16:08

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.