简体   繁体   中英

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. 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. 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} . Hence you end up with "search" as your id when calling GET with the URL provided.

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.

I fixed it by changing api to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM