AmbiguousMatchException: The request matched multiple endpoints. Matches: ApiBurgerNew.Controllers.BurgerController.GetBurgerByCat (ApiBurgerNew) ApiBurgerNew.Controllers.BurgerController.GetBurgerById (ApiBurgerNew)
My controller :
[ApiController]
[Route("api")]
public class BurgerController : ControllerBase
{
private readonly BurgerContext _context;
private readonly IConfiguration _configuration;
public BurgerController(BurgerContext context, IConfiguration configuration)
{
_context = context;
_configuration = configuration;
}
[HttpGet]
public async Task<IActionResult> GetBurger()
{
var burger = await _context.Burger.ToListAsync();
return Ok(burger);
}
//By category
[HttpGet("{cat}")]
public async Task<IActionResult> GetBurgerByCat(string cat)
{
var requete =
from b in _context.Burger
join c in _context.Category
on b.CategoryId equals c.CategoryId
select new { b.BurgerId, b.Name, b.Description, b.Price, c.Category_Name };
if (!string.IsNullOrEmpty(cat))
requete = requete.Where(p => p.Category_Name.Contains(cat));
return Ok(requete);
}
//By Id
[HttpGet("{id}")]
public async Task<IActionResult> GetBurgerById(int id)
{
var burger = await _context.Burger.FindAsync(id);
if (burger == null)
{
return NotFound();
}
return Ok(burger);
}
}
I tried to modify my routes but without success
CodePudding user response:
GetBurgerByCat and GetBurgerById both use the same route. change it like this ( although this API route naming is not a good practice, it's better to use query ):
[ApiController]
[Route("api")]
public class BurgerController : ControllerBase
{
private readonly BurgerContext _context;
private readonly IConfiguration _configuration;
public BurgerController(BurgerContext context, IConfiguration configuration)
{
_context = context;
_configuration = configuration;
}
[HttpGet]
public async Task<IActionResult> GetBurger()
{
var burger = await _context.Burger.ToListAsync();
return Ok(burger);
}
//By category
[HttpGet("bycat/{cat}")]
public async Task<IActionResult> GetBurgerByCat(string cat)
{
var requete =
from b in _context.Burger
join c in _context.Category
on b.CategoryId equals c.CategoryId
select new { b.BurgerId, b.Name, b.Description, b.Price, c.Category_Name };
if (!string.IsNullOrEmpty(cat))
requete = requete.Where(p => p.Category_Name.Contains(cat));
return Ok(requete);
}
//By Id
[HttpGet("byuser/{id}")]
public async Task<IActionResult> GetBurgerById(int id)
{
var burger = await _context.Burger.FindAsync(id);
if (burger == null)
{
return NotFound();
}
return Ok(burger);
}
}
CodePudding user response:
thank you very much, but when I run it, I get the following resultenter image description here