繁体   English   中英

如何解决 Web API 中的多端点错误

[英]How to solve multiple endpoints error in Web API

我在 API 的 controller 中有多个操作。 它们的意思是检索一堆与模型相关的数据,所以它们都是[HttpGet]类型的。 如何解决多个端点错误?

[HttpGet("{genreName}")]
        public IActionResult GetMoviesByGenre(string genreName)
        {
            var movies = _applicationDbContext.Movies.Where(m => m.Genres.Any(g => g.Name == genreName)).ToList();
            if (string.IsNullOrWhiteSpace(genreName))
               return NotFound();
            return Ok(movies);
        }
        [HttpGet("{directorName}")]
        public IActionResult GetMoviesOfADirector(string directorName)
        {
            var movies = _applicationDbContext.Movies.Where(m => m.Director.Name.Contains(directorName)).ToList();
            if (string.IsNullOrEmpty(directorName))
                return NotFound();
            return Ok(movies);

        }

您可以尝试创建两个控制器,例如 DirectorController.cs 和 GenreController,并且您可以拥有如下端点:

api/导演/{directorName}/电影

api/流派/{流派名称}/电影

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM