簡體   English   中英

任務<iactionresult>得到錯誤的值</iactionresult>

[英]Task<IActionResult> is getting the wrong value

[ProducesResponseType(200, Type = typeof(OperationResponse<Plan>))]
        [ProducesResponseType(400, Type = typeof(OperationResponse<Plan>))]
        [HttpGet("{id}")]
        public async Task<IActionResult> Get([FromRoute]  string id)
        {
            string userId = User.FindFirst(ClaimTypes.NameIdentifier).Value;
            System.Diagnostics.StackTrace t = new System.Diagnostics.StackTrace();
            Console.WriteLine("stack is " + t);
            var plan = await _plansService.GetPlanById(id, userId);
            if (plan == null)
                return BadRequest(new OperationResponse<string>
                {
                    IsSuccess = false,
                    Message = "Invalid operation",
                });

            return Ok(new OperationResponse<Plan>
            {
                Record = plan,
                Message = "Plan retrieved successfully!",
                IsSuccess = true,
                OperationDate = DateTime.UtcNow
            });
        }

字符串 id 應該是 guid 類型,而我得到的是“搜索”這個詞,而不是 id,它應該是特定產品的 guid。 如何跟蹤值....或映射?

這是錯誤

您的 URL /api/plans/search?query=you&page=1沒有任何可以解析為 GUID 的內容。 大概 controller 上的路由前綴是/api/plans/所以在你的操作中將id聲明為FromRoute你有一個/api/plans/{id}的路由。 因此,在使用提供的 URL 調用GET時,您最終會以“搜索”作為您的id

您可以嘗試以下方法:

[HttpGet("search")]
public async Task<IActionResult> Get([FromQuery] string id)

然后調用如下:

GET /api/plans/search?id=<guid_string>&page=1`

然后將id設置為可以解析為 guid 的字符串。

我通過將 api 更改為來修復它

var response = await client.GetProtectedAsync<ProductsCollectionPagingResponse>($"{_baseUrl}/api/plans/query={query}/page={page}");

 var response = await client.GetProtectedAsync<ProductsCollectionPagingResponse>($"{_baseUrl}/api/plans?query={query}&page={page}");

暫無
暫無

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

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