Dto对象更新方法不在Postman中更新

时间:2017-04-01 20:18:06

标签: asp.net asp.net-web-api postman

我在我创建的asp.net web api中使用了automapper和Dtos获取,创建,删除和更新方法,但出于某种原因,我无法让更新方法在邮递员中工作。 我选择Put in postman并在其中一个表中更改值,例如

{
    "id": 3,
    "movieTitle": "Movie 1",
    "genreId": 3,
    "releaseDate": "1977-05-02T00:00:00"
  }

  {
    "id": 3,
    "movieTitle": "Movie 11",
    "genreId": 3,
    "releaseDate": "1977-05-02T00:00:00"
  }

但是当我在邮递员中点击发送时,值永远不会更新。 我不明白自己做错了什么,因为我的身份也一样。所有其他方法都有效,但不是更新方法。没有异常/错误被抛出。非常感谢任何帮助。

            [HttpPut]
            public IHttpActionResult UpdateMovie(int id, MovieDto movieDto)
            {
                if (!ModelState.IsValid)
                    return BadRequest();

                var movieInDb = _context.Movie.SingleOrDefault(c => c.Id == id);

                if (movieInDb == null)
                    return NotFound();

                Mapper.Map(movieDto, movieInDb);

                _context.SaveChanges();

                return Ok();
            }

全班

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using AutoMapper;
using Vidly1.Dtos;
using Vidly1.Models;

namespace Vidly1.Controllers.Api
{
    public class MoviesController : ApiController
    {
        private ApplicationDbContext _context;

        public MoviesController()
        {
            _context = new ApplicationDbContext();



        }


        public IEnumerable<MovieDto> getMovies()
        {
            return _context.Movie.ToList().Select(Mapper.Map<Movie, MovieDto>);


        }

        [HttpGet]
        public IHttpActionResult GetMovie(int id)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest();


            }

            var movie = _context.Movie.SingleOrDefault(m => m.Id == id);


            if (movie == null)
            {
                return NotFound();

            }

            return Ok(Mapper.Map<Movie, MovieDto>(movie));


        }

        [HttpPost]
        public IHttpActionResult CreateMovie(MovieDto movieDto)
        {

            if (!ModelState.IsValid)
            {
                return BadRequest();

            }

            var movie = Mapper.Map<MovieDto, Movie>(movieDto);
            if (movie == null)
            {
                return NotFound();

            }

            _context.Movie.Add(movie);
            _context.SaveChanges();
            movieDto.Id = movie.Id;
            return Created(new Uri(Request.RequestUri + "/" + movie.Id), movieDto);
        }


        [HttpPut]
        public IHttpActionResult UpdateMovie(int id, MovieDto movieDto)
        {
            if (!ModelState.IsValid)
                return BadRequest();

            var movieInDb = _context.Movie.SingleOrDefault(c => c.Id == id);

            if (movieInDb == null)
                return NotFound();

            Mapper.Map(movieDto, movieInDb);

            _context.SaveChanges();

            return Ok();
        }


        [HttpDelete]
        public void DeleteMovie(int id, MovieDto movieDto)
        {
            if (!ModelState.IsValid)
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);

            }
            var movie = _context.Movie.SingleOrDefault(m => m.Id == id);

            if (movie == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);

            }

            _context.Movie.Remove(movie);
            _context.SaveChanges();

        }

    }

}

0 个答案:

没有答案