简体   繁体   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
            });
        }

string id should be of type guid and instead I'm getting the word 'search' instead of id which should be a guid for a particular product.字符串 id 应该是 guid 类型,而我得到的是“搜索”这个词,而不是 id,它应该是特定产品的 guid。 How do I trace the value.... or the mapping?如何跟踪值....或映射?

这是错误

Your URL /api/plans/search?query=you&page=1 has nothing that could be parsed as a GUID in it.您的 URL /api/plans/search?query=you&page=1没有任何可以解析为 GUID 的内容。 Presumably the route prefix on the controller is /api/plans/ so in declaring id as FromRoute in your action you have a route of /api/plans/{id} .大概 controller 上的路由前缀是/api/plans/所以在你的操作中将id声明为FromRoute你有一个/api/plans/{id}的路由。 Hence you end up with "search" as your id when calling GET with the URL provided.因此,在使用提供的 URL 调用GET时,您最终会以“搜索”作为您的id

You could try the following:您可以尝试以下方法:

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

Then call it as follows:然后调用如下:

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

id will then be set to a string that can be parsed as a guid.然后将id设置为可以解析为 guid 的字符串。

I fixed it by changing api to我通过将 api 更改为来修复它

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

from

 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