I have a fairly simple API that, when hitting one of the endpoints, gives me the following stack trace
Microsoft.AspNetCore.Routing.Matching.AmbiguousMatchException: The request matched multiple endpoints. Matches:
VimTube.Controllers.CategoryController.GetAllCategories (VimTube) VimTube.Controllers.CategoryController.GetCategoryByName (VimTube)
I understand that it is failing because I have two methods that point to the same URL. The same are the following
[HttpGet]
public Task<ActionResult<IEnumerable<Category>>> GetAllCategories()
{
return _repository.FindAll();
}
[HttpGet]
public IEnumerable<Category> GetCategoryByName([FromQuery]string name)
{
return _repository.ListCategoriesWhichStartsWith(name);
}
And my controller class is prefixed with vimtube/categories .
My idea would be to have these url's
- vimtube/categories returns a list of all categories.
- vimtube/categories?name=Ent returns the categories that start with the query you passed to it.
I read that with [FromQuery] this was possible, but apparently it is not. Thank you!
You could specify routes for each method you create using
[Route("ruta")]
, which would be something likeNow if you want to include parameters in the Uri you can do it like this
[Route("ruta1/{param1:int}")]
where it is clear that you can specify the data type, although I have only used them withstring
eint
.And the last thing, I don't remember using [FromQuery] for the parameters, I was limited to [FromBody] and [FromUri]
Let us know how it goes :)