コード例 #1
0
        // POST api/product
        public HttpResponseMessage Post(ProductsRequest request)
        {
            if (ModelState.IsValid)
            {
                var products = db.AddProducts(request);

                var response = Request.CreateResponse(HttpStatusCode.Created, new DataSourceResult {
                    Data = products
                });
                response.Headers.Location = new Uri(Url.Link("DefaultApi", null));
                return(response);
            }
            else
            {
                var errors = ModelState.Values.SelectMany(v => v.Errors).Select(error => error.ErrorMessage);

                return(Request.CreateResponse(HttpStatusCode.BadRequest, errors));
            }
        }