コード例 #1
0
        public IHttpActionResult PutNews(int id, NewsVm newsVm)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var news = newsVm.ToNews();

            if (id != news.Id)
            {
                return(BadRequest());
            }

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

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!NewsExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
コード例 #2
0
        public IHttpActionResult PostNews(NewsVm newsVm)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var news = newsVm.ToNews();

            db.News.Add(news);
            db.SaveChanges();

            return(CreatedAtRoute("PostNews", new { id = news.Id }, NewsVm.FromNews(news)));
        }