コード例 #1
0
        public IHttpActionResult Postquestion(question question)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            var id = 0;
            try
            {
                id = db.questions.Max(p => p.quesID) + 1;
            }
            catch(Exception exp)
            {
                id = 1;
            }

            question.postedOn = DateTime.Now;
            question.quesID = id;

            db.questions.Add(question);
            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateException)
            {
                if (questionExists(question.quesID))
                {
                    return Conflict();

                }
                else
                {
                    throw;
                }
            }

            return CreatedAtRoute("DefaultApi", new { id = question.quesID }, question);
        }
コード例 #2
0
        public IHttpActionResult Putquestion(int id, question question)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != question.quesID)
            {
                return BadRequest();
            }

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

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!questionExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }