简体   繁体   中英

FromQuery receiving null arguments from HttpClient GetAsync

Why is the IDictionary<string, string> data in API Controller coming in as 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. But its coming in as null. I tried the same on postman If I use [FromQuery] string token instead of [FromQuery] IDictionary<string,string> it works just fine. But I would like the arguments in keyvalue pair format. I need that [FromQuery] to work if I wanted to pass multiple arguments. Currently I am passing only the token but I also need the ID. Hoping to have this keyvalue pair working. 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

 [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. Like you saw parameter:"token" value: "your token value".

So you don't receive it like IDictionary you should receive it as string parameter.

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. If you don't like it then use 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) 
....

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