如何从web api发送dataSet响应并在数据集对象

时间:2017-08-03 07:14:57

标签: asp.net-mvc asp.net-web-api

我正在尝试从Web API发送数据集响应,并希望通过将Controller响应从Api转换为数据集,将Json方法作为数据集接收。

- API方法:

public DataSet GetAllProducts()
        {
            //return products;

            var ds = new DataSet();
            var conn = DACUtil.GetConnection();
            try
            {
                SqlCommand cmd = new SqlCommand("Select * from Employee, Company where Employee.Emp_id='2' and Company.Emp_id=2", conn);
                var da = new SqlDataAdapter { SelectCommand = cmd };
                ds.Tables[0].TableName = "Employee";
                ds.Tables[1].TableName = "Company";      
                da.Fill(ds);
                ds.AcceptChanges();

            }
            finally
            {
                conn?.Dispose();
            }
            return ds;
        }

- 控制器接收方法:

using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://localhost:57589");
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));  
                var response = client.GetAsync("api/products").Result; 
                if (response.IsSuccessStatusCode)
                {
                    using (HttpContent content = response.Content)
                    { 
                        string jsonS=await content.ReadAsStringAsync(); 
                        var ds = JsonConvert.DeserializeObject<DataSet>(jsonS); 
                    }
}

JsonS在Json收到回复,但var ds = JsonConvert.DeserializeObject<DataSet>(jsonS);在转换中抛出异常。

0 个答案:

没有答案