简体   繁体   中英

async methode how can i bind values to IEnumarable

Here i get some values from server

public class iAuth
    {
        public string resultStatus { get; set; }
        public string userName { get; set; }

    }

This IAuth i need to bind my data

private async Tas<bool> GetValiedSession(string _SesToken)
    {
        string Baseurl = WebConfigurationManager.AppSettings["Baseurl"];
        var values = new Dictionary<string, string>{
                      { "productId",  WebConfigurationManager.AppSettings["productId"] },
                      { "productKey",  WebConfigurationManager.AppSettings["productKey"] },
                      { "userName", "gosoddin" },
                      { "securityToken",_SesToken  },
                      };
        using (var client = new HttpClient())
        {
            var _json = JsonConvert.SerializeObject(values);
            var content = new StringContent(_json, Encoding.UTF8, "application/json");
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            var response = await client.PostAsync(Baseurl + "validate/session", content);              
            //var responseString = await response.Content.ReadAsStringAsync();
           IEnumerable<iAuth> aa  = await response.Content.ReadAsStringAsync(); ;
            //  return Ok(responseString);
            return true;
        }

    }

Here how can i bind values to IEnumarable<iAuth> Here im getting Error as cant convert String to system.colllection.Generic

You are trying to read the content of your response to IEnumerable<>

IEnumerable<iAuth> aa  = await response.Content.ReadAsStringAsync();

But ReadAsStringAsync() returns string so that's why the error comes.

So you need to deserialize your Response.Content to a specific type like,

string response  = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<iAuth>(response);

Now you can get resultStatus and userName by using like,

string status = result.resultStatus;
string name = result.userName;

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