I'm trying to call a Web API from an MVC project to check to see if an employee has access to the app. I call a Web API passing the employee number and the app name. If the they have access it will return the EmployeeId, AppName, and AppRoll. If they don't have access it will send back an empty body.
I created a Model called EmployeeAccess that has the 3 properties along with the method to get call the API. When I run it I get an error "Cannot deserialize the current JSON array".
In the example from the link above they are not doing anything with creating JSON objects. Anyone know what I'm missing or doing wrong?
HomeController (some values are hard coding for testing)
public async System.Threading.Tasks.Task<ActionResult> Index()
{
string employeeId = "762041";
string appName = "AppAdmin";
var objEmployeeAccess = new EmployeeAccess();
EmployeeAccess employeeAccess = await objEmployeeAccess.GetEmployeeAccess(employeeId, appName);
return View();
}
Class
public class EmployeeAccess
{
HttpClient client = new HttpClient();
public string AppName { get; set; }
public string Emp { get; set; }
public string Role { get; set; }
internal async Task<EmployeeAccess> GetEmployeeAccess(string employeeId, string appName)
{
string uri = ConfigurationManager.AppSettings["ApiUri"];
string endPoint = string.Format("api/v1/GetAppRoleForEmp?EmpID={0}&AppName={1}", employeeId, AppName);
client.BaseAddress = new Uri(uri);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
EmployeeAccess employee = null;
HttpResponseMessage response = await client.GetAsync(endPoint);
response.EnsureSuccessStatusCode();
if (response.IsSuccessStatusCode)
{
employee = await response.Content.ReadAsAsync<EmployeeAccess>();
}
return employee;
}
}
This is what is returned when found.

UPDATE - changed to using a List<> to get it work
public async System.Threading.Tasks.Task<ActionResult> Index()
{
string employeeId = "762041";
string appName = "AppAdmin";
var employeeAccess = new EmployeeAccess();
List<EmployeeAccess> listEmployee = await employeeAccess.GetEmployeeAccess(employeeId, appName);
return View();
}
public class EmployeeAccess
{
HttpClient client = new HttpClient();
public string AppName { get; set; }
public string Emp { get; set; }
public string Role { get; set; }
internal async Task<EmployeeAccess> GetEmployeeAccess(string employeeId, string appName)
{
string uri = ConfigurationManager.AppSettings["ApiUri"];
string endPoint = string.Format("api/v1/GetAppRoleForEmp?EmpID={0}&AppName={1}", employeeId, appName);
client.BaseAddress = new Uri(uri);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync(endPoint);
response.EnsureSuccessStatusCode();
List<EmployeeAccess> listEmployee = new List<EmployeeAccess>();
if (response.IsSuccessStatusCode)
{
listEmployee = await response.Content.ReadAsAsync<List<EmployeeAccess>>();
}
return listEmployee;
}
}
