title: Return an object along with a 409 Conflict error in a Web API 2 POST call backed by Entity Framework?
body: <p>I have a C# Entity Framework <strong>Web API 2 controller</strong>.  Currently when an attempt is made via the <strong>POST</strong> method to create an object with the same text for the main text field, I return a <strong>409 Conflict error</strong> as an <strong>StatusCode</strong> result to indicate the addition is considered a duplicate.</p>
  
  <p>What I'd like to do is return the server side object that triggered the duplicate error too.  So I need something akin to the <strong>Ok()</strong> method but a variant that returns a <strong>409 Conflict</strong> error as the HTTP status code <em>instead of an HTTP OK status code</em>.  </p>
  
  <p>Is there such a thing?  How can I do this?  If I can make this work the client doesn't have to do a subsequent <strong>Get</strong> call to the server to get the existing object after receiving a 409 Conflict error.</p>
  
  <p>Here's the current POST method:</p>
  
  <pre><code>    public IHttpActionResult PostCanonical(Canonical canonical)
      {
          if (!ModelState.IsValid)
          {
              return BadRequest(ModelState);
          }
  
          // Check for duplicate Canonical text for the same app name.
          if (db.IsDuplicateCanonical(canonical.AppName, canonical.Text))
          {
              // It's a duplicate.  Return an HTTP 409 Conflict error to let the client know.
              return StatusCode(HttpStatusCode.Conflict);
          }
  
          db.CanonicalSentences.Add(canonical);
          db.SaveChanges();
  
          return CreatedAtRoute("DefaultApi", new { id = canonical.ID }, canonical);
      }
  </code></pre>