Handling PATCH updates in a Web API service

I was working on a project where my client wanted a Web API service so various external portals could add and update members in CRM. Doing this I came across a problem called PATCH.

In the REST world it is very common to update items by only sending the changed data and leaving out the other variables. Let's imagine a "contact" entity where I only want to update the first name and leave the last name unchanged.

My REST call would only contain the first name and I wouldn't even send the last name along with the request. I would try something like this:

public IHttpActionResult UpdateContact(Guid contactId, Contact contact)
    using (var context = new CrmServiceContext(CrmService))
        var c = new Contact();
        c.Id = contactId;

        if (contact.FirstName != null)
            c.FirstName = contact.FirstName;

        if (contact.LastName != null)
            c.LastName = contact.LastName;

        return Ok();

The problem in this case would be that I cannot see if the variable wasn't sent along or if the user wanted to empty the value by sending a null value.

Here the Delta class from the Microsoft ASP.NET WebAPI OData comes in handy..

After you install the NuGet package you can change the contact object to Delta<Contact> and this way you have access to the GetChangedPropertyNames() property.

public IHttpActionResult UpdateContact(Guid contactId, Delta<Contact> contact)
    using (var context = new CrmServiceContext(CrmService))
        var changedProperties = contact.GetChangedPropertyNames().ToList();

        var c = new Contact();
        c.Id = contactId;

        if (changedProperties.Contains("FirstName"))
            object changedProperty;
            contact.TryGetPropertyValue("FirstName", out changedProperty);
            c.FirstName = Convert.ToString(changedProperty);

        if (changedProperties.Contains("LastName"))
            object changedProperty;
            contact.TryGetPropertyValue("LastName", out changedProperty);
            c.LastName = Convert.ToString(changedProperty);

        return Ok();

Now I can determine with the changedProperties.Contains("PropertyName") if a property has changed and if I should empty it or leave it unchanged.

Beware this only appears to work when you post a JSON request with the Content-Type header properly set to application/json. I tried various ways to get it working with posted form data but the changedProperties list stays empty.

Comments (10) -

  • CathyRoberson

    10/19/2016 7:11:09 AM |

    I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It's always nice when you can not only be informed, but also entertained! I'm sure you had fun writing this article.

  • essay writing agency

    1/4/2017 9:36:45 AM |

    My husband is a programmer and I guess he will help me in it.

  • I unfortunately do not understand the programming. But I think that the characteristics of this accomodation it will be very useful for my friend. I share with them a link to your post.

  • buy an essay online

    1/28/2017 3:42:21 PM |

    That was something I wasn't aware about. I hope to see her more materials on that. It will be helpful.

  • http://edit-ing.services/

    2/8/2017 7:12:18 PM |

    Thanks, that was useful. I hope that you'll enlarge this topic, so I could make it by myself.

  • writing an essay

    4/17/2017 8:49:31 PM |

    I'll try to make it your way. Thank you for sharing those variants with us. I do appreciate your work!

  • OwVuM

    4/19/2017 4:22:10 AM |

    809723 350734We�re glad to become visitor on this pure site, regards in this rare info! 783136

  • check my essay - essay grammar check

    5/10/2017 11:48:05 AM |

    This is too complicated for me. However, I will do my best to figure out what is what. Your explanation is clear and exact, so I rely on your professionalism in this field.

  • website to bet

    5/16/2017 8:36:35 AM |

    612039 975235As being a Newbie, We�re permanently exploring online for articles which can be of help to me. Many thanks 500049

  • 40779 335600You produced some decent points there. I looked on the net for any difficulty and located most individuals goes along with together along with your web site. 68814

  • wanktube.com

    5/17/2017 4:43:33 PM |

    43480 528169The good intreguing articles keep me coming back here time and time again. thank you so much. 961723

  • Shemale escorts London

    5/18/2017 3:12:04 PM |

    614776 526335You made various good points there. I did a search on the topic and found most people will have the same opinion with your blog. 966579

  • browse around here

    5/20/2017 9:19:16 AM |

    206828 309442When I originally commented I clicked the "Notify me when new comments are added" checkbox and now each time a comment is added I get three emails with the same comment. Is there any way you can remove me from that service? Thanks a lot! 640897

  • wholesale workwear clothing

    5/20/2017 2:15:47 PM |

    411905 890910Thank you for the great writeup. It in fact was a amusement account it. Look advanced to far added agreeable from you! Nevertheless, how could we communicate? 592639

  • mca

    5/22/2017 5:12:33 AM |

    719367 370924Some genuinely    marvelous  function on behalf with the owner of this  internet internet site ,  dead wonderful   topic matter. 656291

  • Unterbet guenstig

    5/23/2017 1:31:18 PM |

    53117 116110This website is truly a walk-through for all of the info you wanted about it and didn�t know who to question. Glimpse here, and you�ll definitely discover it. 767568

  • www.marshallsmobileautorepair.com

    5/24/2017 2:48:56 AM |

    889931 693167There is noticeably a bundle to learn about this. I assume you created positive excellent points in capabilities also. 742232

  • Lyrics

    5/24/2017 12:35:49 PM |

    557814 630056Does your blog have a contact page? I�m having a tough time locating it but, I�d like to send you an e-mail. I�ve got some suggestions for your blog you might be interested in hearing. Either way, fantastic web site and I look forward to seeing it expand over time. 798920

  • prix ouverture de porte paris

    5/24/2017 2:56:27 PM |

    492865 546465Some  truly  quality    weblog posts on this  internet web site ,  saved to my bookmarks . 29783

  • jocuri friv

    5/24/2017 3:20:46 PM |

    110600 425138I was trying to find this. Really refreshing take on the information. Thanks a lot. 872956