コード例 #1
0
        // PUT api/Lyricist/5
        public HttpResponseMessage PutLyricist(int id, Lyricist lyricist)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (id != lyricist.Id)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

            db.Entry(lyricist).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }
コード例 #2
0
        // POST api/Lyricist
        public HttpResponseMessage PostLyricist(Lyricist lyricist)
        {
            if (ModelState.IsValid)
            {
                db.Lyricists.Add(lyricist);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, lyricist);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = lyricist.Id }));
                return response;
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }