如何从Wep API获取多个返回对象值?

时间:2016-04-19 07:07:30

标签: c#

我的Wep API方法:

    [HttpGet]
            public object getData()
            {
              var firstObj = dbContext.Customer();
              var secondObj= dbContext.Department();
              var thirdObj= dbContext.Email();
              return new { firstObj,secondObj,thirdObj };
            }

    /* this is my client side call */
     using (var client = new HttpClient())
      {
       client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", CommonHelper.CurrentToken);
         client.BaseAddress = new Uri(CommonHelper.baseAddress);
         HttpResponseMessage response = await client.GetAsync("/OPUS/Accounts/getData");
     response.EnsureSuccessStatusCode(); 
}

我从不同的表中返回了多个对象。我需要在WEP API

中从单个方法中获取这些值

2 个答案:

答案 0 :(得分:1)

尝试下面的内容,传回一个包含足够值的匿名对象:

container-fluid
   row
    col-xs-10 col-sm-8 col-md-6 col-lg-4
      title
       container-fluid
          row 
            col-md-6 col-lg-6
             table1
            col-md-6 col-lg-6
              table2
          row
            col-lg-12
             table3

答案 1 :(得分:0)

我可以通过单一的WEB API方法获取所有对象。

using (var client = new HttpClient())
{
       client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", CommonHelper.CurrentToken);
       client.BaseAddress = new Uri(CommonHelper.baseAddress);
       HttpResponseMessage response = await client.GetAsync("/OPUS/Accounts/getData");
       response.EnsureSuccessStatusCode(); 

var Lookups = await response.Content.ReadAsAsync<object>();
                    JObject _jObject = JObject.Parse(Lookups.ToString());

                    JArray deptStatus = _jObject["firstObj"] as JArray;
                    DeptTypeLookups = deptStatus .ToObject<ObservableCollection<Department>>();

JArray custStatus = _jObject["secondObj"] as JArray;
                    custTypeLookups = custStatus .ToObject<ObservableCollection<CustDetail>>();
}

由于