コード例 #1
0
        // POST api/Answer
        public HttpResponseMessage PostUserAnswer(UserAnswer useranswer)
        {
            useranswer.User = this.User.Identity.Name;

            if (ModelState.IsValid)
            {
                db.UserAnswers.Add(useranswer);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, useranswer);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = useranswer.UserAnswerID }));
                return response;
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }
コード例 #2
0
        // PUT api/Answer/5
        public HttpResponseMessage PutUserAnswer(int id, UserAnswer useranswer)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (id != useranswer.UserAnswerID)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

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

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

            return Request.CreateResponse(HttpStatusCode.OK);
        }