I am trying to create a very basic controller with three get methods Below are the three uri's for them
- GET /movies - gets all movies.
- GET /movies?name={name}` - finds all movies matching the specified name
- Get /Movies/{Id} - Find movie By Id
My code for the controller is as below:
[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)
{
var result = await moviesService.GetMovieById(Id);
return Ok(result);
}
}
When I send the request to GetById by api/movies/31234567-89ab-cdef-0123-456789abcdef then it works
but for api/movies and api/movies?name=Test I get below error:
The request matched multiple endpoints. Matches: MoviesController.Get and MoviessController.GetByName
Can anyone please suggest me what is the best way to implement such scenario in web API .net core 3.1 considering best practises?
