Ejemplo n.º 1
0
        public IHttpActionResult Update(long id, MyEntity entity)
        {
            if (entity == null) throw new InvalidDataException("Missing request body.");
            if (!ModelState.IsValid) throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            if (entity.Id != id) throw new InvalidDataException($"ID in URI ({id}) must match the ID in the body ({entity.Id}).");

            _service.Update(entity);

            return StatusCode(HttpStatusCode.NoContent);
        }
Ejemplo n.º 2
0
        public IHttpActionResult Create(MyEntity entity)
        {
            if (!ModelState.IsValid) return BadRequest(ModelState);
            if (entity == null) return BadRequest("Missing request body.");

            _service.Add(entity);

            return Created(
                location: new Uri(Request.RequestUri.EnsureTrailingSlash(), entity.Id.ToString()),
                content: entity);
        }