Json.NET 和 RESTSharp - 转换值时出错



我创建了一个 API,我现在正在尝试通过另一个应用程序访问它。即使我使用 Newtonsoft.JSON 来序列化和反序列化 JSON,我也遇到了转换错误,并出现以下 InnerException:

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

以下是引发错误的代码段(在 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;
    }

这里称为:

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

该信息来自以下 API 控制器操作(在单独的应用程序中):

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

两个应用程序中的 DTO 是相同的:

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

我尝试使用json2csharp工具进行响应,在我看来一切似乎都很好:

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

任何建议将不胜感激。

编辑:

RegisterViewModel 类 公共类注册视图模型 {

    public RegistrationDTO regModel { get; set; }
    public List<ManagerDTO> availableManager { get; set; }
    public List<PropertyDTO> availableProperties { get; set; }
    public List<TrainerDTO> availableTrainers { get; set; }
}

使控制器 Get() 返回类型IHttpActionResult

并返回Ok(dtoList);

相关内容

  • 没有找到相关文章

最新更新