无法从 Web api OK() 响应中提取模型对象
Unable to extract model object from web api OK() response
我有模型class
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public string Designation { get; set; }
public string Department { get; set; }
public DateTime Dateofjoin { get; set; }
public string Phone { get; set; }
public string Address { get; set; }
public int IsActive { get; set; }
public int row_number { get; set; }
}
另一个模型 class 使用 'Employee' 模型 class 作为列表
public class EmployeeList
{
public IList<Employee> Emp_List { get; set; }
public int Return_Param { get; set; }
}
我正在调用 api 方法来获取员工记录以及 return 参数。
public IHttpActionResult Get_Employee()
{
if (!ModelState.IsValid)
throw new HttpResponseException(HttpStatusCode.BadRequest);
var displaydata = db.Get_Employee();
return Ok(displaydata);
}
这里显示'EmployeeList'模型class的数据returns对象,成功得到Emp_List和Return_Param。
我有另一个控制器,从那里调用 api 方法并从 OK 方法 returned.
中提取响应
public ActionResult Get_Employee()
{
EmployeeList empobj = new EmployeeList();
try
{
empobj.Emp_List = new List<Employee>();
HttpClient hc = new HttpClient();
hc.BaseAddress = new Uri(baseURL);
var consumeapi = hc.GetAsync("EmployeeApi");
consumeapi.Wait();
var readdata = consumeapi.Result;
if (readdata.IsSuccessStatusCode)
{
empobj.Emp_List = readdata.Content.ReadAsAsync<List<Employee>>().Result;
empobj.Return_Param = Convert.ToInt32(readdata.Content.ReadAsStringAsync());
}
}
catch (WebException ex)
{
if (ex.Status == WebExceptionStatus.Timeout) { }
else throw;
}
return View("EmployeeList",empobj);
}
但我无法提取模型对象,而是获取无法反序列化为 json 的异常。我怎么能得到 empobj.Emp_List 和 empobj.Return_Param if(readdata.IsSuccessStatusCode)?
var consumeapi = hc.GetAsync("EmployeeApi");
调用了您的 Get_Employee()
api 方法,对吗?
我认为您的代码需要
if (readdata.IsSuccessStatusCode)
{
empobj = readdata.Content.ReadAsAsync<EmployeeList>().Result;
}
我有模型class
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public string Designation { get; set; }
public string Department { get; set; }
public DateTime Dateofjoin { get; set; }
public string Phone { get; set; }
public string Address { get; set; }
public int IsActive { get; set; }
public int row_number { get; set; }
}
另一个模型 class 使用 'Employee' 模型 class 作为列表
public class EmployeeList
{
public IList<Employee> Emp_List { get; set; }
public int Return_Param { get; set; }
}
我正在调用 api 方法来获取员工记录以及 return 参数。
public IHttpActionResult Get_Employee()
{
if (!ModelState.IsValid)
throw new HttpResponseException(HttpStatusCode.BadRequest);
var displaydata = db.Get_Employee();
return Ok(displaydata);
}
这里显示'EmployeeList'模型class的数据returns对象,成功得到Emp_List和Return_Param。 我有另一个控制器,从那里调用 api 方法并从 OK 方法 returned.
中提取响应 public ActionResult Get_Employee()
{
EmployeeList empobj = new EmployeeList();
try
{
empobj.Emp_List = new List<Employee>();
HttpClient hc = new HttpClient();
hc.BaseAddress = new Uri(baseURL);
var consumeapi = hc.GetAsync("EmployeeApi");
consumeapi.Wait();
var readdata = consumeapi.Result;
if (readdata.IsSuccessStatusCode)
{
empobj.Emp_List = readdata.Content.ReadAsAsync<List<Employee>>().Result;
empobj.Return_Param = Convert.ToInt32(readdata.Content.ReadAsStringAsync());
}
}
catch (WebException ex)
{
if (ex.Status == WebExceptionStatus.Timeout) { }
else throw;
}
return View("EmployeeList",empobj);
}
但我无法提取模型对象,而是获取无法反序列化为 json 的异常。我怎么能得到 empobj.Emp_List 和 empobj.Return_Param if(readdata.IsSuccessStatusCode)?
var consumeapi = hc.GetAsync("EmployeeApi");
调用了您的 Get_Employee()
api 方法,对吗?
我认为您的代码需要
if (readdata.IsSuccessStatusCode)
{
empobj = readdata.Content.ReadAsAsync<EmployeeList>().Result;
}