不明确匹配异常:请求与多个终结点匹配。匹配:ApiBurgerNew.控制器.汉堡控制器.按猫获取汉堡(ApiBurgerNew)ApiBurgerNew.控制器.汉堡控制器.按ID获取汉堡(ApiBurgerNew)
我的控制器:
[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
1条答案
按热度按时间wkftcu5l1#
GetBurgerByCat和GetBurgerById都使用相同的路由。请按如下方式更改它(尽管这种API路由命名不是一种好的做法,但最好使用query):