JsonPatch in an Angular/.NET Web API project

2019-05-19 01:37发布

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?

0条回答
登录 后发表回答