Advertisement
Ludwiq

PUT controller

Nov 16th, 2014
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.85 KB | None | 0 0
  1. // PUT: api/Users/5
  2.         [ResponseType(typeof(void))]
  3.         public async Task<IHttpActionResult> PutUser(int id, User user)
  4.         {
  5.             if (!ModelState.IsValid)
  6.             {
  7.                 return BadRequest(ModelState);
  8.             }
  9.  
  10.             if (id != user.Id)
  11.             {
  12.                 return BadRequest();
  13.             }
  14.  
  15.             db.Entry(user).State = EntityState.Modified;
  16.  
  17.             try
  18.             {
  19.                 await db.SaveChangesAsync();
  20.             }
  21.             catch (DbUpdateConcurrencyException)
  22.             {
  23.                 if (!UserExists(id))
  24.                 {
  25.                     return NotFound();
  26.                 }
  27.                 else
  28.                 {
  29.                     throw;
  30.                 }
  31.             }
  32.  
  33.             return StatusCode(HttpStatusCode.NoContent);
  34.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement