带有 OData $count 的 .Net Core 3 Web API 未返回值
.Net Core 3 Web API with OData $count is not returning a value
我有一个工作网络 API,它响应 GET 请求和 returns 报告列表。我最近实现了 Odata 和选项,例如 $filter 和 $top,当我使用 $count 时它似乎被忽略了。
我已经阅读了多个博客和表格,但我无法让它发挥作用。
Startup.cs
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; private set; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(o =>
{
o.ReturnHttpNotAcceptable = true;
o.EnableEndpointRouting = false;
});
// Connection string from environmental variables
var connectionString = Configuration["connectionString:077test"];
// Connection to SQL
services.AddDbContext<ServiceCatalogContext>(option => option.UseSqlServer(connectionString));
// Scope
services.AddScoped<IReportsRepo, ReportsRepo>();
services.AddControllers();
services.AddOData();
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler(appBuilder =>
{
appBuilder.Run(async context =>
{
context.Response.StatusCode = 500;
await context.Response.WriteAsync("An unexpected fault happened. Try again later.");
});
});
}
//app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
app.UseStatusCodePages();
//app.UseMvc();
//expanding app.UseMvc for oData
app.UseMvc(routeBuilder =>
{
//routeBuilder.EnableDependencyInjection();
routeBuilder.Select()
.OrderBy()
.Filter()
.SkipToken()
.MaxTop(10)
.Expand()
.Count();
routeBuilder.MapODataServiceRoute("api", "api", GetEdmModel());
});
}
private static IEdmModel GetEdmModel()
{
var odataBuilder = new ODataConventionModelBuilder();
odataBuilder.EntitySet<ReportsDto>("reports");
return odataBuilder.GetEdmModel();
}
}
ReportsController.cs
public class ReportsController : ODataController //Controller //ControllerBase
{
private readonly IReportsRepo _reportsRepo;
public ReportsController(IReportsRepo reportsRepo, IMapper mapper)
{
_reportsRepo = reportsRepo ??
throw new ArgumentNullException(nameof(reportsRepo)); //handle if resultsRepo is null
}
[HttpGet]
[EnableQuery()] //enabled OData querying
[ODataRoute("reports")]
public ActionResult<IQueryable<ReportsDto>> GetReports() //updated from IEnumerable to IQueryable when OData was added
{
var results = _reportsRepo.GetReports();
//return Ok(_mapper.Map<IEnumerable<ReportsDto>>(results)); //mapper not working with OData if I change IEnumerable to IQueryable
return Ok(results);
}
}
API returns 结果,我可以使用 $filter 和 $top,但我就是不知道如何让 $count 工作。
任何对我的具体 API 的帮助将不胜感激!
使用 app.UseEndpoints()
而不是 app.UseMvc()
。你的 UseMvc()
相当于:
app.UseEndpoints(endpoints =>
{
//endpoints.EnableDependencyInjection();
endpoints.Select()
.OrderBy()
.Filter()
.SkipToken()
.MaxTop(10)
.Expand()
.Count();
endpoints.MapODataRoute("api", "api", GetEdmModel());
});
我有一个工作网络 API,它响应 GET 请求和 returns 报告列表。我最近实现了 Odata 和选项,例如 $filter 和 $top,当我使用 $count 时它似乎被忽略了。
我已经阅读了多个博客和表格,但我无法让它发挥作用。
Startup.cs
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; private set; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(o =>
{
o.ReturnHttpNotAcceptable = true;
o.EnableEndpointRouting = false;
});
// Connection string from environmental variables
var connectionString = Configuration["connectionString:077test"];
// Connection to SQL
services.AddDbContext<ServiceCatalogContext>(option => option.UseSqlServer(connectionString));
// Scope
services.AddScoped<IReportsRepo, ReportsRepo>();
services.AddControllers();
services.AddOData();
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler(appBuilder =>
{
appBuilder.Run(async context =>
{
context.Response.StatusCode = 500;
await context.Response.WriteAsync("An unexpected fault happened. Try again later.");
});
});
}
//app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
app.UseStatusCodePages();
//app.UseMvc();
//expanding app.UseMvc for oData
app.UseMvc(routeBuilder =>
{
//routeBuilder.EnableDependencyInjection();
routeBuilder.Select()
.OrderBy()
.Filter()
.SkipToken()
.MaxTop(10)
.Expand()
.Count();
routeBuilder.MapODataServiceRoute("api", "api", GetEdmModel());
});
}
private static IEdmModel GetEdmModel()
{
var odataBuilder = new ODataConventionModelBuilder();
odataBuilder.EntitySet<ReportsDto>("reports");
return odataBuilder.GetEdmModel();
}
}
ReportsController.cs
public class ReportsController : ODataController //Controller //ControllerBase
{
private readonly IReportsRepo _reportsRepo;
public ReportsController(IReportsRepo reportsRepo, IMapper mapper)
{
_reportsRepo = reportsRepo ??
throw new ArgumentNullException(nameof(reportsRepo)); //handle if resultsRepo is null
}
[HttpGet]
[EnableQuery()] //enabled OData querying
[ODataRoute("reports")]
public ActionResult<IQueryable<ReportsDto>> GetReports() //updated from IEnumerable to IQueryable when OData was added
{
var results = _reportsRepo.GetReports();
//return Ok(_mapper.Map<IEnumerable<ReportsDto>>(results)); //mapper not working with OData if I change IEnumerable to IQueryable
return Ok(results);
}
}
API returns 结果,我可以使用 $filter 和 $top,但我就是不知道如何让 $count 工作。
任何对我的具体 API 的帮助将不胜感激!
使用 app.UseEndpoints()
而不是 app.UseMvc()
。你的 UseMvc()
相当于:
app.UseEndpoints(endpoints =>
{
//endpoints.EnableDependencyInjection();
endpoints.Select()
.OrderBy()
.Filter()
.SkipToken()
.MaxTop(10)
.Expand()
.Count();
endpoints.MapODataRoute("api", "api", GetEdmModel());
});