Пример #1
0
        public IHttpActionResult Put(int id, Book book)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState);
                }

                if (book == null || id != book.Id)
                {
                    return BadRequest("Please provide valid book data");
                }

                this.db.Entry(book).State = EntityState.Modified;
                try
                {
                    this.db.SaveChanges();
                }
                catch (DBConcurrencyException)
                {
                    return NotFound();
                }

                return Ok(book);
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }
        }
Пример #2
0
        public IHttpActionResult Post(Book book)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState);
                }

                if (book == null)
                {
                    return BadRequest("Please provide valid book data");
                }

                this.db.Books.Add(book);
                this.db.SaveChanges();

                return Created(
                    new Uri(Url.Link("DefaultApi", new { id = book.Id })),
                    book);
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }
        }