示例#1
0
        // POST api/task
        public HttpResponseMessage Post(ParticipantTask task)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            using (CrowdContext db = new CrowdContext())
            {
                db.ParticipantTasks.Add(task);
                db.SaveChanges();
                var response = Request.CreateResponse(HttpStatusCode.Created, task);
                //response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = task.Key }));
                return response;
            }
        }
示例#2
0
        // PUT api/task/5
        public HttpResponseMessage Put(int id, ParticipantTask task)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
            if (id != task.Id)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

            using (CrowdContext db = new CrowdContext())
            {
                db.ParticipantTasks.Attach(task);
                db.Entry(task).State = EntityState.Modified;
                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
                }
                return Request.CreateResponse(HttpStatusCode.OK);
            }
        }