title: Web API and Upserts
body: <p>I'm working with Web API, and I'm trying to create an action that will perform an upsert.</p>
  
  <p>I am passing in a user's first name and last name, and if that combination exists in my table, I want to update that user. If the combination does not exist, I want to create that user. (Yes, this is contrived, but for a blind wine-tasting party app I'm building, security isn't necessary)</p>
  
  <p>Given that Web API has mapped POST and PUT to create and update operations respectively, it feels like there is a bit of friction here. I'm using the POST operation exclusively, should I be doing this in another way?</p>
  
  <pre><code>    // POST api/User
      [ResponseType(typeof(User))]
      public async Task&lt;IHttpActionResult&gt; PostUser(User user)
      {
          if (!ModelState.IsValid)
          {
              return BadRequest(ModelState);
          }
  
          var current = await db.Users.FirstOrDefaultAsync(u =&gt; u.FirstName == user.FirstName &amp;&amp; u.LastName == user.LastName);
          int id;
          if (current == null)
          {
              db.Users.Add(user);;
              await db.SaveChangesAsync();
              id = user.ID;
          }
          else
          {
              id = current.ID;
              //Do some updates
              current.Prop1 = "val";
              await db.SaveChangesAsync();
              user = current;
          }
  
          return CreatedAtRoute("DefaultApi", new { id = id }, user);
      }
  </code></pre>
  
  <p>The alternative, I suppose, would be to let the client decide whether it needs to do an insert or an update. That would necessitate another Web API call to determine an object's existence, and then consume the appropriate Web API with the appropriate HTTP verb.</p>