Asp.net 邮递员中未更新Dto对象更新方法

Asp.net 邮递员中未更新Dto对象更新方法,asp.net,asp.net-web-api,postman,Asp.net,Asp.net Web Api,Postman,在我创建的asp.net web api中,我有一个使用automapper和Dtos的get、create、delete和update方法,但由于某些原因,我无法在postman中使用update方法。 我选择Put in postman并更改其中一个表中的值,例如 { "id": 3, "movieTitle": "Movie 1", "genreId": 3, "releaseDate": "1977-05-02T00:00:00" } 到 但当我点击

在我创建的asp.net web api中,我有一个使用automapper和Dtos的get、create、delete和update方法,但由于某些原因,我无法在postman中使用update方法。 我选择Put in postman并更改其中一个表中的值,例如

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

但当我点击send in postman时,这些值从未更新。 我不明白我做错了什么,因为我有相同的id。所有其他方法都在工作,但更新方法不起作用。没有引发异常/错误。非常感谢您的帮助

            [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();

        }

    }

}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
Net系统;
使用System.Net.Http;
使用System.Web.Http;
使用自动制版机;
使用Vidly1.Dtos;
使用Vidly1.模型;
命名空间Vidly1.Controllers.Api
{
公共类电影控制器:ApiController
{
私有应用程序的bContext\u上下文;
公共电影总监()
{
_context=新的ApplicationDbContext();
}
公共IEnumerable getMovies()
{
返回_context.Movie.ToList().Select(Mapper.Map);
}
[HttpGet]
公共IHttpActionResult GetMovie(int id)
{
如果(!ModelState.IsValid)
{
返回请求();
}
var movie=\u context.movie.SingleOrDefault(m=>m.Id==Id);
如果(电影==null)
{
返回NotFound();
}
返回Ok(Mapper.Map(movie));
}
[HttpPost]
公共IHttpActionResult CreateMovie(MovieDto MovieDto)
{
如果(!ModelState.IsValid)
{
返回请求();
}
var movie=Mapper.Map(movieDto);
如果(电影==null)
{
返回NotFound();
}
_context.Movie.Add(电影);
_SaveChanges();
movieDto.Id=movie.Id;
创建的返回(新Uri(Request.RequestUri+“/”+movie.Id),movieDto);
}
[HttpPut]
公共IHttpActionResult UpdateMovie(int id,MovieDto MovieDto)
{
如果(!ModelState.IsValid)
返回请求();
var movieInDb=_context.Movie.SingleOrDefault(c=>c.Id==Id);
if(movieInDb==null)
返回NotFound();
Mapper.Map(movieDto,movieInDb);
_SaveChanges();
返回Ok();
}
[HttpDelete]
public void DeleteMovie(int-id,MovieDto-MovieDto)
{
如果(!ModelState.IsValid)
{
抛出新的HttpResponseException(HttpStatusCode.BadRequest);
}
var movie=\u context.movie.SingleOrDefault(m=>m.Id==Id);
如果(电影==null)
{
抛出新的HttpResponseException(HttpStatusCode.NotFound);
}
_context.Movie.Remove(电影);
_SaveChanges();
}
}
}

1)您的请求是什么样的?2) 行动达成了吗?3) 这些值是否绑定到您的方法?3) 你已经完成调试器了吗?最后的退出路径是什么?当我单击“发送”时,我看到的唯一消息是在邮递员内。输出为{“message”:“请求的资源不支持http方法‘PUT’”}我还看到状态405方法不允许您使用属性路由还是基于约定的路由?这与您如何配置路由以及如何命名操作有关。因此,现在您需要调查一条错误消息。在这里,还有其他的。
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();

        }

    }

}