簡體   English   中英

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。

我在同一個 controller 中有另一個請求:這個

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

它工作正常。 所以我不明白第一個有什么問題。

該請求返回 404,因為端點期望deviceIdentifier參數是Path Parameter而不是Query Parameter

在這種情況下,您應該將請求 url 更改為https://localhost:5002/Main/Authenticate/bd78209e-e3a0-4576-a17c-832838ce6495或刪除路由到[HttpPost("Authenticate")] 2838ce6495"。

從您的請求路徑中刪除 {deviceIdentifier} 參數

[HttpPost("認證/{deviceIdentifier}")]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM