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

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

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

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

            return Request.CreateResponse(HttpStatusCode.OK);
        }
コード例 #2
0
        // POST api/SingleModel
        public HttpResponseMessage PostSingleModel(SingleModel singlemodel)
        {
            if (ModelState.IsValid)
            {
                db.SingleModels.Add(singlemodel);
                db.SaveChanges();

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