Пример #1
0
        // POST api/Default1
        public HttpResponseMessage PostExpense(Expense expense)
        {
            if (ModelState.IsValid)
            {
                rep.Add(expense);
                rep.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, expense);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = expense.Id }));
                return response;
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }
Пример #2
0
        // PUT api/Default1/5
        public HttpResponseMessage PutExpense(int id, Expense expense)
        {
            if (ModelState.IsValid && id == expense.Id)
            {
                //rep. db.Entry(expense).State = EntityState.Modified;

                try
                {
                    rep.SaveChanges();
                }
                catch (Exception)
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }

                return Request.CreateResponse(HttpStatusCode.OK);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }