简体   繁体   English

异步方法如何将值绑定到IEnumarable

[英]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 我需要绑定此IAuth数据

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 在这里我如何将值绑定到IEnumarable<iAuth>在这里因为无法将String转换为system.colllection.Generic而出现错误

You are trying to read the content of your response to IEnumerable<> 您正在尝试读取对IEnumerable<>响应的内容

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

But ReadAsStringAsync() returns string so that's why the error comes. 但是ReadAsStringAsync()返回string所以这就是错误发生的原因。

So you need to deserialize your Response.Content to a specific type like, 因此,您需要将Response.Content反序列化为特定的类型,例如,

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

Now you can get resultStatus and userName by using like, 现在,您可以使用like获得resultStatususerName

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

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

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