在Asp.net Core Web Api中,如果需要编写多个Controller方法来获取所有数据,则可以按照以下步骤进行操作:
[HttpGet]
public IEnumerable GetAllProducts()
{
return _context.Products.ToList();
}
services.AddControllers()
.AddNewtonsoftJson();
services.AddTransient();
services.AddTransient();
注意,需要注入对应的Controller类。
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
/api/{ControllerName}
例如,访问所有产品数据的API将如下所示:
/api/Products