.net 我无法访问我的产品ID和我的产品类别,这里是我的控制器,我有以下错误消息:

gab6jxml  于 2022-12-01  发布在  .NET
关注(0)|答案(1)|浏览(75)

不明确匹配异常:请求与多个终结点匹配。匹配: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
wkftcu5l

wkftcu5l1#

GetBurgerByCat和GetBurgerById都使用相同的路由。请按如下方式更改它(尽管这种API路由命名不是一种好的做法,但最好使用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);
        }
    }

相关问题