Ejemplo n.º 1
0
        // PUT api/Country/5
        public HttpResponseMessage PutCountry(int id, Country country)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (id != country.CountryId)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

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

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

            return Request.CreateResponse(HttpStatusCode.OK);
        }
Ejemplo n.º 2
0
        // POST api/Country
        public HttpResponseMessage PostCountry(Country country)
        {
            if (ModelState.IsValid)
            {
                db.Countries.Add(country);
                db.SaveChanges();

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