Check which properties will be update (which came from request body) OData c#

Viewed 275

this my update endpoint, I want to do something before performing the update, so how I can detect which properties will update or which properties from request body?

public override async Task<IActionResult> Put([FromBody] CategoryTree update)
{
   // check which properties update
   return await base.Update(update);
}
2 Answers

Compare the request body values against the object you have stored in the database if you want to use it for other logic.

If you're just wanting to update the modified values for an object, Entity Framework handles most of that with change tracking.

I can think in some different approaches:

1️⃣ Using a reduced ViewModel of CategoryTree for your endpoint with only the fields involved in the request. ==> This can be painful since you will need to use different endpoints for different Forms/pages, but maybe is the more ordered way of doing it.

2️⃣ Inspect the Request object to check which are the Form parameters send to the endpoint (not an easy task, but can be done)

3️⃣ Implement some Custom Model Binding to be able to convert your endpoint to something like this:

public override async Task<IActionResult> Put([FromBody] CategoryTree update, List<string> involvedProperties)

Then, in your custom Model Binder you have to fill the List<string> involvedProperties parameter according to what is coming in the request.

Related