小编Nis*_*ava的帖子

.net core 3.1 web api 中的多个 get 方法

我正在尝试使用三个 get 方法创建一个非常基本的控制器以下是它们的三个 uri

  1. GET /movies - 获取所有电影。
  2. GET /movies?name={name}` - 查找与指定名称匹配的所有电影
  3. Get /Movies/{Id} - 按 ID 查找电影

我的控制器代码如下

[Route("api/[controller]")]
[ApiController]
public class MoviesController : ControllerBase
{
    private readonly IMoviesService moviesService;

    public MoviesController(IMoviesService moviesService)
    {
        this.moviesService = moviesService;
    }
    
    [HttpGet]
    public async Task<IActionResult> Get()
    {
        var result = await moviesService.GetMoviesAsync();
        return Ok(result);
    }
    
    [HttpGet]
    public async Task<IActionResult> GetByName([FromQuery(Name = "name")] string name)
    {
        var result = await moviesService.GetMoviesByNameAsync(name);
        return Ok(result);
    }

    [HttpGet("{Id}", Name = "GetById")]
    public async Task<IActionResult> GetById(Guid Id) …
Run Code Online (Sandbox Code Playgroud)

.net api .net-core asp.net-core

5
推荐指数
2
解决办法
6012
查看次数

标签 统计

.net ×1

.net-core ×1

api ×1

asp.net-core ×1