0

I have created an API that I am now attempting to access through another application. Even though I am using Newtonsoft.JSON to both serialize and deserialize the JSON, I am encountering a conversion error, with the following InnerException:

{"Could not cast or convert from System.String to System.Collections.Generic.List`1[MidamAPI.Models.ManagerDTO]."}

The following is the snippet that throws the error (in RESTFactory):

     public List<T> Execute<T>(String endPoint) where T : new() {
        RestClient client = new RestClient(BASE_URL);

        IRestRequest req = new RestRequest(endPoint);
        req.AddHeader("Authorization", "Bearer " + this._token);
        var response = client.Execute(req).Content;
        List<T> responseData = JsonConvert.DeserializeObject<List<T>>(response);

        return responseData;
    }

which is called here:

{
        RegisterViewModel vm = new RegisterViewModel();
        RESTFactory factory = new RESTFactory((String)Session["bearer"]);
        vm.availableManager = factory.Execute<ManagerDTO>("managers");
        vm.availableProperties = factory.Execute<PropertyDTO>("locations");
        vm.availableTrainers = factory.Execute<TrainerDTO>("trainers");

        return View(vm);
    }

The information is coming from the following API controller action (in a separate application):

    [LocalFilter]
[RoutePrefix("managers")]
public class ManagersController : ApiController
{
    UnitOfWork worker = new UnitOfWork();

       [Route("")]
    public string Get()
    {
        IEnumerable<ManagerDTO> dtoList = Mapper.Map<List<Manager>, List<ManagerDTO>>(worker.ManagerRepo.Get().ToList());

        foreach (ManagerDTO dto in dtoList)
        {
            Manager manager = worker.ManagerRepo.GetByID(dto.ID);
            dto.Stores = (Mapper.Map<IEnumerable<Property>, IEnumerable<PropertyDTO>>(manager.Properties)).ToList();
        }

        return JsonConvert.SerializeObject(dtoList);
    }

The DTOs are the same in both applications:

{
public class ManagerDTO
{
    public int ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public List<PropertyDTO> Stores { get; set; }
}




  public class PropertyDTO
{
    public int ID;
    public string ShortName { get; set; }
    public string Street { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }
    public string Phone { get; set; }
    public string Lat { get; set; }
    public string Long { get; set; }
    public string GooglePlaceID { get; set; }
    public string PropertyNumber { get; set; }
    public int ManagerID { get; set; }
    public int TrainerID { get; set; }

    public string ManagerName { get; set; }
    public string ManagerEmail { get; set; }
    public string TrainerEmail { get; set; }

}

I tried using the json2csharp tool with the response, and everything seems fine to my eyes:

public class Store
{
    public int ID { get; set; }
    public string ShortName { get; set; }
    public string Street { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }
    public string Phone { get; set; }
    public string Lat { get; set; }
    public string Long { get; set; }
    public string GooglePlaceID { get; set; }
    public string PropertyNumber { get; set; }
    public int ManagerID { get; set; }
    public int TrainerID { get; set; }
    public string ManagerName { get; set; }
    public string ManagerEmail { get; set; }
    public object TrainerEmail { get; set; }
}

public class RootObject
{
    public int ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public List<Store> Stores { get; set; }
}

Any advice would be appreciated.

EDIT:

The RegisterViewModel class public class RegisterViewModel {

    public RegistrationDTO regModel { get; set; }
    public List<ManagerDTO> availableManager { get; set; }
    public List<PropertyDTO> availableProperties { get; set; }
    public List<TrainerDTO> availableTrainers { get; set; }
}
4
  • can we see what RegisterViewModel looks like? Commented Oct 31, 2017 at 14:00
  • Certainly, the model has been added. Thanks. Commented Oct 31, 2017 at 14:03
  • Is the JSON returned from the API properly formatted? Commented Oct 31, 2017 at 14:14
  • The JSON appears proper. The json2charp tool was able to produce classes appropriately, and I am able to access the api through both PHP and Javascript clients. Commented Oct 31, 2017 at 14:17

1 Answer 1

1

Make your controller Get() return type IHttpActionResult

and return Ok(dtoList);

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

2 Comments

Thanks. I gave it a try, but it still returned the same error unfortunately.
Sorry, you dont need to serailize the object just return return Ok(dtoList);

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.