ASP.NET Core 6 Odata Swagger UI总是显示$ count查询
我用Swagger创建了一个新的ASP.NET Core Web API项目,并添加了odatacontroller
。 Swagger UI按预期显示了我的用户
路由,并且有效。但是Swagger UI也意外地显示用户/$ COUNT
:
为什么$ count
那里?我可以防止它出现吗?
我正在使用Microsoft.aspnetcore.odata 8.0.10(最新)。我使用swashbuckle.aspnetcore 6.2.3(模板默认)和6.3.1(最新)获得了相同的结果。
我的整个控制器:
public class UsersController : ODataController
{
private readonly ErpContext _db;
public UsersController(ErpContext db)
{
_db = db;
}
[HttpGet]
public IActionResult Get()
{
return Ok(_db.Users);
}
}
我的整个EDM:
public static class EntityDataModel
{
public static IEdmModel Build()
{
var builder = new ODataConventionModelBuilder();
builder.EntitySet<User>("Users");
return builder.GetEdmModel();
}
}
我的整个创业公司,如果有一些顺序敏感性或其他内容:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContextFactory<ErpContext>(options =>
{
options.UseSqlServer(builder.Configuration["ConnectionStrings:DefaultConnection"]);
});
builder.Services
.AddControllers()
.AddOData(options =>
{
options.AddRouteComponents("odata", EntityDataModel.Build());
});
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
请注意,我的控制器操作没有[enableQuery]
和我的odataoptions
dik没有count()
或任何其他查询。除非我添加这些,否则不需要的$ count
查询甚至行不通。
我已经在a
I created a new ASP.NET Core Web API project with Swagger and added an ODataController
. The Swagger UI shows my Users
route as expected, and it works. But the Swagger UI also unexpectedly shows Users/$count
:
Why is $count
there? Can I prevent it from appearing?
I'm using Microsoft.AspNetCore.OData 8.0.10 (the latest). I get the same results with Swashbuckle.AspNetCore 6.2.3 (the template default) and 6.3.1 (the latest).
My entire controller:
public class UsersController : ODataController
{
private readonly ErpContext _db;
public UsersController(ErpContext db)
{
_db = db;
}
[HttpGet]
public IActionResult Get()
{
return Ok(_db.Users);
}
}
My entire EDM:
public static class EntityDataModel
{
public static IEdmModel Build()
{
var builder = new ODataConventionModelBuilder();
builder.EntitySet<User>("Users");
return builder.GetEdmModel();
}
}
My entire startup, in case there's some order sensitivity or something:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContextFactory<ErpContext>(options =>
{
options.UseSqlServer(builder.Configuration["ConnectionStrings:DefaultConnection"]);
});
builder.Services
.AddControllers()
.AddOData(options =>
{
options.AddRouteComponents("odata", EntityDataModel.Build());
});
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
Note that my controller action does not have [EnableQuery]
and my ODataOptions
does not have Count()
or any other query. The unwanted $count
query doesn't even work unless I add those.
I've reproduced the problem in a minimal project with an in-memory DB.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论