ASP.NET Core 6 Odata Swagger UI总是显示$ count查询

发布于 2025-02-03 05:18:41 字数 2016 浏览 2 评论 0原文

我用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:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文