无法获取带参数的路线

Iva*_*rez 1 c# asp.net-mvc

我正在尝试创建一个简单的产品控制器类,每当我调用api/products /我得到"列表记录"就好了,但是当我尝试调用api/products/1时我得到404找不到,我做错了什么.

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");
        }



    }    
}
Run Code Online (Sandbox Code Playgroud)

Nin*_*rry 6

不使用

    [HttpGet("/{id:int}")]
Run Code Online (Sandbox Code Playgroud)

因为斜线指的是路径的根.而是使用

    [HttpGet("{id:int}")]
Run Code Online (Sandbox Code Playgroud)