无法将类型dto转换为Ienumerable类型

时间:2019-01-23 00:00:32

标签: asp.net-core-webapi

我正在创建一个asp.net核心web.api,需要从Ienumerable方法返回DTO对象。我在控制器get方法中收到错误消息,说无法将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
                };
            }
        }
}

1 个答案:

答案 0 :(得分:1)

尝试这种方式

return new DTO.Products[]{  
    DTO.Products { Id = 1, Name = "Cricket bat", Available = DateTime.Now, Code = "AC122333", Price = 1223.23M }
};