JsonPatch in an Angular/.NET Web API project

十年热恋 提交于 2019-12-10 11:31:11

问题


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?

来源:https://stackoverflow.com/questions/52527311/jsonpatch-in-an-angular-net-web-api-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!