【发布时间】:2021-06-06 16:11:44
【问题描述】:
所以现在它会匹配路由 api/cmets 的两个动作,我希望第二个是 api/cmets?blogId=1,但我不希望它是 api/cmets/{blogId }。
//Get api/comments
[HttpGet]
[Route("/")]
public async Task<IActionResult> GetAll()
{
var comments = await _context.Comments.ToListAsync();
if(comments != null)
return Ok(new { status = 200, comments });
return NotFound();
}
//Get api/comments?blogId=1
[HttpGet]
public async Task<IActionResult> GetCommentsBy(int blogId)
{
var allComments = await _context.Comments.ToListAsync();
if (allComments != null)
{
var commentsByBlogId = allComments.Where(c => c.BlogId == blogId);
return Ok(new { status = 200, comments = commentsByBlogId });
}
return NotFound();
}
【问题讨论】:
标签: c# .net asp.net-web-api2 webapi