NET 核心 webapi:为什么我收到这个特定 API 请求的 404?

NET core webapi: Why am I getting a 404 with this specific API request?

我有这个请求

    [HttpPost("Authenticate/{deviceIdentifier}")]
    public async Task<ActionResult<ServiceResponse<AuthorizedDoorsDto>>> Authenticate(Guid deviceIdentifier)
    {
        Guid id = Guid.Parse(User.Claims.FirstOrDefault(d => d.Type == ClaimTypes.NameIdentifier).Value);
        return Ok(await mainService.Authenticate(id, deviceIdentifier));
    }

我正在使用 Postman 对此进行测试并发送此请求:

https://localhost:5002/Main/Authenticate?deviceIdentifier=bd78209e-e3a0-4576-a17c-832838ce6495

但我得到了 404。

我在同一个控制器中有另一个请求:这个

    //For debugging
    [HttpGet("GetAll")]
    public async Task<ActionResult<Device>> GetAll()
    {
        Guid id = Guid.Parse(User.Claims.FirstOrDefault(d => d.Type == ClaimTypes.NameIdentifier).Value);
        return Ok(await mainService.GetDevice(id));
    }

并且运行良好。所以我不明白第一个有什么问题。

请求 returns 404 因为端点期望 deviceIdentifier 参数是 路径参数 而不是 查询参数.

在这种情况下,您应该将请求 url 更改为 https://localhost:5002/Main/Authenticate/bd78209e-e3a0-4576-a17c-832838ce6495 或从到 [HttpPost("Authenticate")].

的路由中删除此参数

从您的请求路径中删除 {deviceIdentifier} 参数

[HttpPost("Authenticate/{deviceIdentifier}")]