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 (29) -

  • 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

  • Alliance FX Capital Singapore

    5/27/2017 10:11:44 PM |

    603568 510134very good day, your internet internet site is low cost. I do a lot of thanks for succeed 591

  • Automobiltechnik

    5/27/2017 10:16:17 PM |

    760939 635393Hi there, just became aware of your weblog by way of Google, and identified that it is genuinely informative. I�m gonna watch out for brussels. I�ll be grateful in case you continue this in future. A lot of men and women is going to be benefited from your writing. Cheers! 197719

  • 878944 765725I�d should verify with you here. Which isn�t something I often do! I take pleasure in reading a post that might make individuals believe. Moreover, thanks for allowing me to comment! 633836

  • Makalah

    5/28/2017 7:41:43 PM |

    74892 968160You must participate in a contest for among the best blogs on the web. I will suggest this web site! 323461

  • Alliancefx.capital

    5/29/2017 10:08:51 PM |

    36273 801705Does 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. 888138

  • airsoft bio bbs

    5/30/2017 8:13:27 AM |

    106843 229225Greetings! Quick question that�s completely off topic. Do you know how to make your site mobile friendly? My weblog looks weird when viewing from my iphone. I�m trying to find a template or plugin that might be able to fix this problem. If you have any recommendations, please share. Appreciate it! 721316

  • best restaurants in atlanta ga

    5/30/2017 9:34:03 AM |

    794698 536439 Youre so cool! I dont suppose Ive read anything like this before. So good to uncover somebody with some original thoughts on this topic. realy thank you for starting this up. this internet site is something that is necessary on the internet, someone with slightly originality. beneficial job for bringing something new towards the internet! 114379

  • 992277 821879Youre so cool! I dont suppose Ive read anything in this way before. So nice to uncover somebody with some original suggestions on this subject. realy appreciate starting this up. this outstanding web site is something that is required over the internet, a person if we do originality. valuable work for bringing something new towards the web! 319173

  • Alliance FX Capital Singapore

    5/31/2017 10:11:33 PM |

    945325 569020As I internet site  possessor I  believe  the  topic material  here is  rattling  amazing ,  appreciate it for your efforts. 383342

  • money loans

    6/2/2017 8:18:55 AM |

    184928 815755As I site  possessor I feel  the  content material material  here is  actually   superb ,  appreciate it for your efforts. 484940

  • Schilcherland Realitäten e.U.

    6/2/2017 8:41:27 PM |

    40917 706792I undoubtedly didn�t recognize that. Learnt some thing new nowadays! Thanks for that. 734958

  • Alliance FX Capital

    6/3/2017 10:10:23 PM |

    417278 166457Great  post, I  conceive  blog owners  should  acquire a lot from this  web blog  its  real  user  pleasant. 933905

  • Toddler

    6/4/2017 5:03:47 PM |

    305412 504547I like what you guys are up too. Such smart function and reporting! Carry on the superb works guys I?ve incorporated you guys to my blogroll. I feel it will improve the value of my internet web site 251779

  • Ucapan Lebaran Idul Fitri 2017

    6/5/2017 3:34:44 PM |

    622820 884877Heya just wanted to give you a brief heads up and let you know several with the pictures aren�t loading properly. I�m not positive why but I feel its a linking concern. I�ve tried it in two different internet browsers and both show exactly the same outcomes. 335613

  • Alliance FX Capital Singapore

    6/5/2017 10:07:53 PM |

    668290 749938Most appropriate the human race messages work to show your and present exclusive chance with special couple. Beginer appear system in advance of raucous individuals will most likely always be aware most of the golden value off presentation, which is really a person�s truck. greatest man jokes 458946

  • Alliance FX Capital Singapore

    6/7/2017 10:09:58 PM |

    644257 970079thank you  dearly  author  , I  located oneself  this internet web site  really  beneficial and its full of  superb  healthy  selective details ! , I  as properly thank you for  the wonderful  food strategy post. 596474

  • Anonymous currency

    6/8/2017 2:33:48 PM |

    68373 960182dog grooming will be the specialty of my sister, she truly loves grooming every dog in our house   668425

  • The Christ Gospel Church Cult 2017

    6/10/2017 2:51:47 PM |

    731894 887995Some times its a discomfort inside the ass to read what men and women  wrote but this internet site   is  real  user  genial ! . 248111

  • Christ Gospel Church Cults

    6/10/2017 2:58:27 PM |

    701771 934031You made some decent points there. I looked on the internet for that issue and located most people will go in addition to with the web website. 271600

  • click here now

    6/10/2017 3:26:06 PM |

    67701 376203Thanks for the info provided! I was researching for this write-up for a long time, but I was not able to see a dependable source. 88800

  • energie v domacnosti

    6/12/2017 3:04:58 PM |

    884720 667345hey there i stumbled upon your site searching around the web. I wanted to tell you I enjoy the look of things around here. Keep it up will bookmark for sure. 329547

  • netfly.se

    6/14/2017 4:18:39 AM |

    856830 513098dress shops that offer discounts are really common in our location and i always shop at them,. 642285

  • laptop deals

    6/19/2017 7:50:21 PM |

    335903 24712Exceptional weblog here! Furthermore your internet site rather a good deal up rapidly! What host are you the usage of? Can I get your affiliate link to your host? I wish my internet site loaded up as rapidly as yours lol 240155

  • breast implants surgery

    6/20/2017 6:40:37 AM |

    Thanks for posting this info. I just want to let you know that I just check out your site and I find it very interesting and informative. I can't wait to read lots of your posts.

  • beste proteinpulver

    6/21/2017 12:05:37 AM |

    290903 826320An intriguing discussion will probably be worth comment. I believe that you can write read much more about this topic, might nicely definitely be a taboo subject but generally folks are inadequate to chat on such topics. To a higher. Cheers 979946

  • kosttilskudd

    6/21/2017 12:42:42 AM |

    172101 251996Ich kenne einige Leute, die aus  Kanadakommen. Eines Tages werde ich auch dorthin reisen  Lg Daniela 979615

  • tilskuddsguiden.no

    6/21/2017 10:09:38 PM |

    293232 217008You produced some decent points there. I looked on the internet for the dilemma and discovered most individuals will go coupled with along along with your internet site. 206818

  • sendFlowersToSerbia

    10/2/2017 11:50:08 AM |

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

  • Gift To Pakistan

    11/25/2017 11:16:17 AM |

    I learn from a good blog, your blog I have a great inspiration, thank you.

  • Custom Essay Writing Service

    11/30/2017 6:05:36 AM |

    Excellent Blog! I would like to thank for the efforts you have made in writing this post. I am hoping the same best work from you in the future as well. I wanted to thank you for this websites! Thanks for sharing. Great websites!

  • Academic Writing Style

    11/30/2017 6:06:25 AM |

    Thank you so much for share such a wonderful post. I enjoyed a lot and I am very happy for getting the good article because rarely will get this type resources surely I will share this article for my friends because they will get the good tips and information.

  • Academic Writing Style

    11/30/2017 6:07:14 AM |

    I am very happy to read this. This is the kind of manual that needs to be given and not the random misinformation that’s at the other blogs. Appreciate your sharing this best posting.

  • Anna R Grow

    12/21/2017 5:31:49 AM |

    Massive post.  Really good-looking blog. A cluster of blogs, I watch these days don't really present anything that I'm interested in. but I'm most absolutely interest in this one. I am in really happy with article value and route. This post is mark on in helpful how some thought apply to any script point. Thanks a lot for protection enormous things. I am enormously a good deal thankful used designed for this place.

  • Cheap Essay Writing Service

    1/2/2018 6:10:13 AM |

    Thank you so much for this informative post!!
    USAAssignment Help deals into essay help online with the help of assignment expert. We aimed at making cheap essay writing service simple and reduce the burden of students. Visit Link: https://goo.gl/yqAmKR

  • leading line create agencies

    2/9/2018 10:31:26 AM |

    Those updates are in sooth crucial! Thanks for illustrating the process. It's an adorable share!

  • TV price drop alert

    2/22/2018 10:39:30 AM |

    Technology makes people more conscious and informative about the modern world.Technology gives people the whole world in their hand.Technology has developed different software, video game apps, and TV consumer apps much better than old days.Nowadays, technology also has developed most entertainment device, TV.Every day people are checking tv price tracker for their courtesy through technology.

  • academic writing service

    3/8/2018 7:59:31 AM |

    There is a different technique to write an essay but you should follow the path which will lead you to the place where you can find the right way to write the essay. Here Am going to show you about how to write an essay. There is the link to the description which will guide you through the whole process: <a href="https://www.paperown.com ">academic writing service </a>

  • al3abmazra3a

    3/30/2018 6:41:37 PM |

    I will try this updates my friend i think this codes is very well and fun.

  • Cash Advance Corona

    4/1/2018 10:47:45 AM |

    Further, instead of holding up days or half a month for a choice, numerous online organizations will react inside a few hours. Rivalry in this market is savage, so they will do what it takes to get your business.

  • Acid Eye

    4/11/2018 11:19:07 AM |

    Thanks a lot for sharing. I am a magnificent place. I've been there a couple of times and I want again.