asp.net-core-webapi - 无法将类型 dto 转换为类型 Ienumerable
问题描述
我正在创建一个 asp.net 核心 web.api 并且需要从 Ienumerable 方法返回 DTO 对象。我在控制器获取方法中遇到错误,说无法将 DTO 转换为 Ienumerable 类型
namespace Products.DTO
{
public class Products
{
public int Id { get; set; }
public string Name { get; set; }
public string Code { get; set; }
public DateTime Available { get; set; }
public decimal Price { get; set; }
}
}
namespace Products.API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ProductsController : ControllerBase
{
[HttpGet]
public ActionResult<IEnumerable<DTO.Products>> GetProducts()
{
return new DTO.Products
{
Id = 1, Name = "Cricket bat", Available = DateTime.Now, Code = "AC122333", Price = 1223.23M
};
}
}
}
解决方案
试试这个方法
return new DTO.Products[]{
DTO.Products { Id = 1, Name = "Cricket bat", Available = DateTime.Now, Code = "AC122333", Price = 1223.23M }
};
推荐阅读
- google-analytics - 阻止 Google Analytics 跟踪整个 URL。GA 忽略 page_path,我不想阻止 URL 变量
- single-sign-on - 如何处理 IdentityServer 身份验证机制中的本地用户角色权限?
- string - CLIPS 不会断言来自 READLINE 的值
- javascript - 使用Function调用DataTable,点击获取TD数据,报错“Cannot read property '0' of undefined”
- reactjs - 如何在没有选项列表的情况下使用 react-select?
- python - Python中不同机器的缩写月份名称的差异
- sql - 查找坐标之间的平均距离
- python-3.x - 如何使用工厂男孩创建一个在用户上有 Fk 的实例?
- linux - 使用脚本自动回答 TTY 提示
- python - 使用 C API 和 `object` 的 `tp_basicsize` 从任意 Python 类派生?