コード例 #1
0
        // PUT api/Product/5
        public HttpResponseMessage PutProduct(int id, Product product)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (id != product.ProductId)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

            db.Products.Attach(product);
            db.ObjectStateManager.ChangeObjectState(product, EntityState.Modified);

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

            return Request.CreateResponse(HttpStatusCode.OK);
        }
コード例 #2
0
        // POST api/Product
        public HttpResponseMessage PostProduct(Product product)
        {
            if (ModelState.IsValid)
            {
                db.Products.AddObject(product);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, product);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = product.ProductId }));
                return response;
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }
コード例 #3
0
 /// <summary>
 /// Create a new Product object.
 /// </summary>
 /// <param name="productId">Initial value of the ProductId property.</param>
 /// <param name="productName">Initial value of the ProductName property.</param>
 /// <param name="unitPrice">Initial value of the UnitPrice property.</param>
 public static Product CreateProduct(global::System.Int32 productId, global::System.String productName, global::System.Int32 unitPrice)
 {
     Product product = new Product();
     product.ProductId = productId;
     product.ProductName = productName;
     product.UnitPrice = unitPrice;
     return product;
 }
コード例 #4
0
 /// <summary>
 /// Deprecated Method for adding a new object to the Products EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToProducts(Product product)
 {
     base.AddObject("Products", product);
 }