我正在尝试创建一个简单的产品 Controller 类,每当我调用 api/products/时,我都会得到“列表记录”,但是当我尝试调用 api/products/1 时,我得到 404 not found,我在做什么错了。
namespace api.Controllers
{
[Route("api/[controller]")]
public class productsController : Controller
{
// get record
[HttpGet("/{id:int}")]
public IActionResult GetRecord(int id)
{
return Ok("get record" + id.ToString());
}
// get records
[HttpGet("/")]
[HttpGet("")]
public IActionResult ListRecords()
{
return Ok("list records");
}
}
}
最佳答案
不要使用
[HttpGet("/{id:int}")]
因为斜线指的是路径的根。而是使用
[HttpGet("{id:int}")]
关于c# - 无法获取带参数的路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47985549/