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

时间:2019-02-06 11:12:12

标签: c# asp.net-mvc asp.net-web-api android-asynctask

这里我从服务器上获得了一些值

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

    }

此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;
        }

    }

这是如何将值绑定到IEnumarable<iAuth> 这里由于无法将String转换为system.colllection.Generic而出现错误

1 个答案:

答案 0 :(得分:1)

您正在尝试阅读对IEnumerable<>的回复内容

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

但是ReadAsStringAsync()返回string,所以才出现错误。

因此,您需要将Response.Content反序列化为特定类型,例如

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

现在,您可以使用like获得resultStatususerName

string status = result.resultStatus;
string name = result.userName;
相关问题