简体   繁体   English

FromQuery从HttpClient GetAsync接收null arguments

[英]FromQuery receiving null arguments from HttpClient GetAsync

Why is the IDictionary<string, string> data in API Controller coming in as null?为什么 API Controller 中的 IDictionary<string, string> 数据以 null 的形式出现? I am using HttpClient GetAsync to make a call to the APi Controller.I am sending a one parameter as a querystring https://localhost:44384/my/types?token=NbLeZVEEksQ0GTIY2clmM50uRfZ9%252bWY895mfS25R1zI%253d On the API Controller side, I am using [FromQuery] to receive the token as a keyvale pair. I am using HttpClient GetAsync to make a call to the APi Controller.I am sending a one parameter as a querystring https://localhost:44384/my/types?token=NbLeZVEEksQ0GTIY2clmM50uRfZ9%252bWY895mfS25R1zI%253d On the API Controller side, I am使用 [FromQuery] 将令牌作为 keyvale 对接收。 But its coming in as null.但它以 null 的形式出现。 I tried the same on postman If I use [FromQuery] string token instead of [FromQuery] IDictionary<string,string> it works just fine.我在 postman 上尝试了相同的方法如果我使用 [FromQuery] 字符串令牌而不是 [FromQuery] IDictionary<string,string> 它工作得很好。 But I would like the arguments in keyvalue pair format.但我想要键值对格式的 arguments。 I need that [FromQuery] to work if I wanted to pass multiple arguments.如果我想传递多个 arguments,我需要 [FromQuery] 工作。 Currently I am passing only the token but I also need the ID.目前我只传递令牌,但我还需要 ID。 Hoping to have this keyvalue pair working.希望这个键值对能够正常工作。 https://localhost:44384/note/types?token=73UtMF24W1%252fpUbO5TlF%252bOJ0uRfZ9%252bWY895mfS25R1zI%253d&custid=1} https://localhost:44384/note/types?token=73UtMF24W1%252fpUbO5TlF%252bOJ0uRfZ9%252bWY895mfS25R1zI%253d&custid=1}

              using (var client = new HttpClient())
                {
                    string targetUrl = string.Format("{0}{1}", "http://localhost:4451", "my/types");

                     var builder = new UriBuilder(targetUrl);

                var query = HttpUtility.ParseQueryString(builder.Query);
                query["token"] = encodedSSo;
                query["custid"] = "1";
                builder.Query = query.ToString();
               ;
                using (var response = await client.GetAsync(builder.ToString()))
                {
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        responseBody = await response.Content.ReadAsStringAsync();
                    }
                }
                
                    using (var response = await client.GetAsync(builder.ToString()))
                    {
                        if (response.StatusCode == HttpStatusCode.OK)
                        {
                            responseBody = await response.Content.ReadAsStringAsync();
 
                        }
                    }
                  
                    return Ok(repo.result);
                }

API controller API controller

 [Route("my/types")]
        [HttpGet]
        public IHttpActionResult GetTypesAsync([FromQuery] IDictionary<string, string> data) ///why is the data here null. Should it not be a keyvalue pair because HttpClient GetAsync is sending a keyvalue pair? [FromQuery] string data.. works just fine. 
        {
            try
            {
                //do something 
                return Ok(report);
            }
            catch (Exception ex)
            {
                IHttpActionResult response;

            }
        }

When you send the KeyValuePairs you are assigning the parameter values.当您发送 KeyValuePairs 时,您正在分配参数值。 Like you saw parameter:"token" value: "your token value".就像您看到的参数:“令牌”值:“您的令牌值”。

So you don't receive it like IDictionary you should receive it as string parameter.所以你不会像 IDictionary 那样接收它,你应该将它作为字符串参数接收。

Try to change this line to尝试将此行更改为

public IHttpActionResult GetTypesAsync([FromQuery] string token)

If you what to get list of keyvaluepair from GET request it is the only way you can get it.如果您要从 GET 请求中获取键值对列表,这是您获取它的唯一方法。 If you don't like it then use POST如果您不喜欢它,请使用 POST

[HttpGet("~/my/types")]
public IHttpActionResult GetTypesAsync(string token) 
{
var data = new List<KeyValuePair<string, string>>
           {
            new KeyValuePair<string, string>("token", token)
            };
....

result结果

[{"Key":"token","Value":"NbLeZVEEksQ0GTIY2clmM50uRfZ9%252bWY895mfS25R1zI%253d"}]

and code和代码

var url=@"https://localhost:44384/my/types?token=NbLeZVEEksQ0GTIY2clmM50uRfZ9%252bWY895mfS25R1zI%253";

 using (var response = await client.GetAsync(url))
{
...

UPDATE更新

if you want to use more params, your action should be如果您想使用更多参数,您的操作应该是

[HttpGet("~/my/types")]
public IHttpActionResult GetTypesAsync(Dictionary<string, string> data) 
....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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