// POST api/Tweet public HttpResponseMessage PostTweet(Tweet tweet) { if (ModelState.IsValid) { db.Tweets.Add(tweet); db.SaveChanges(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, tweet); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = tweet.TweetId })); return response; } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } }
// PUT api/Tweet/5 public HttpResponseMessage PutTweet(int id, Tweet tweet) { if (!ModelState.IsValid) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } db.Entry(tweet).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex); } return Request.CreateResponse(HttpStatusCode.OK); }