【发布时间】:2020-08-24 18:46:31
【问题描述】:
我想从 .NET API 中获取数据,并将其包含在参考中。 但是,很糟糕,我遇到了以下异常。
错误 2 无法将类型
System.Threading.Tasks.Task<System.Collections.Generic.List<BizCover.Repository.Cars.Car>>隐式转换为BizCover.Api.Cars.Model.CarsVM
这是我在 API 控制器中的代码。
public IHttpActionResult getAllDataCars()
{
CarRepository carRep = new CarRepository();
IHttpActionResult result = null;
try
{
CarsVM dataAPICars = new CarsVM();
dataAPICars = carRep.GetAllCars(); //here's the error return.
if (dataAPICars != null)
{
var a = "ada";
}
else
{
var b = "null";
}
}
catch (Exception ex)
{
var c = ex.ToString();
}
return result;
}
这也是我的模型
public class CarsVM
{
public CarsVM()
{
}
public string Colour { get; set; }
public string CountryManufactured { get; set; }
public int Id { get; set; }
public string Make { get; set; }
public string Model { get; set; }
public decimal Price { get; set; }
public int Year { get; set; }
public CarsVM(string Colour, string CountryManufactured, int Id, string Make, string Model, decimal Price, int Year)
{
this.Colour = Colour;
this.CountryManufactured = CountryManufactured;
this.Id = Id;
this.Make = Make;
this.Model = Model;
this.Price = Price;
this.Year = Year;
}
}
这里的目标是我想从参考 (CarRepository) 中获取数据并将其存储在我的模型中。 我没有清楚地理解 .NET Framework API 应该如何逐步工作以及如何实现 await async。
【问题讨论】:
-
嗨,您确定产生错误的行了吗?如果有,请在代码中注明
标签: c# asp.net .net api async-await