我有一个模型类
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; }
}
另一个模型类,使用'雇员'模型类作为列表。
public class EmployeeList
{
public IList<Employee> Emp_List { get; set; }
public int Return_Param { get; set; }
}
我正在调用一个api方法来获取员工记录以及一个返回参数,这里displaydata返回了'EmployeeList'模型类的对象,并成功获取了Emp_List和Return_Param。
public IHttpActionResult Get_Employee()
{
if (!ModelState.IsValid)
throw new HttpResponseException(HttpStatusCode.BadRequest);
var displaydata = db.Get_Employee();
return Ok(displaydata);
}
这里displaydata返回'EmployeeList'模型类的对象,并成功得到Emp_List和Return_Param。我有另一个控制器,在那里调用api方法并从返回的OK方法中提取响应。
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;
}