asp.net-web-api - 具有 OData $count 的 .Net Core 3 Web API 未返回值
问题描述
我有一个有效的 Web API,它响应 GET 请求并返回报告列表。我最近实现了 Odata 和 $filter 和 $top 等选项,当我使用 $count 时,它似乎被忽略了。
我已经阅读了多个博客和表格,但我无法让它发挥作用。
启动.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();
}
}
报告控制器.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 返回结果,我可以使用 $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());
});
推荐阅读
- apache-flink - 作业之间的数据/事件交换
- htmlunit - 使用 HtmlUnit 如何单击(形成)不是按钮的按钮
- r - 在 FlexDashboard 中过滤。过滤选项不是数据集的一部分
- reactjs - 当回调在父级中更改状态时,如何使用 React.memo 和 useCallback 优化 React 组件
- git - Git 在上一个差异之后添加
- angular - 角度测试因 takeWhile() 而失败
- laravel - 我从远程服务器上读到了一些难看的字母
- c# - 在 Unity Vuforia 插件中捕获 VideoBackground 的快照
- c# - LINQ to SQL:插入子实体失败,因为 SQL 中不包含 ID?
- c# - 实体框架不刷新数据