1

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. enter image description here

Here is the actual error. enter image description here

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;
    }
}

2 Answers 2

4

You are trying to deserialize a JSON array into a JSON object and you get an error. To avoid this just change this line

await response.Content.ReadAsAsync<EmployeeAccess>();

into this

await response.Content.ReadAsAsync<List<EmployeeAccess>>();

And after you get the result take the first element and return your object.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks...I changed it to using a List<EmployeeAccess> and it looks like that will work. I posted the updated code above.
0

The problem is that your controller is expecting a single object but the API is passing back an array with a single entry.

You can change the API so that it gives back a single document

{
    "AppName": "AppAdmin",
    "Emp": "00762041",
    "Role": "APPADMIN"
}

or you can change your processing such that it processes an array and takes the first element.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.