c# - 如何在 asp.net 核心 WebApi 项目中使用带有 swagger 的 OData 过滤器?

标签 c# asp.net-core swagger odata asp.net-core-webapi

我有一个使用 C# 在 ASP.NET 5/core 和 EntityFrameworkCore 之上编写的 Web API。

我正在使用 OData在查询数据库时应用过滤器。

我需要手动应用 ODataQueryOptions到我的DbSet<>生成分页信息。

这是我的代码

[Route("api/[controller]")]
[ApiController]
public class ProductsController : ControllerBase
{
    private readonly DbContext _db;

    public ProductsController(DbContext db)
    {
        _db = db;
    }

    [HttpGet]
    [EnableQuery(AllowedQueryOptions = AllowedQueryOptions.All, AllowedFunctions = AllowedFunctions.AllFunctions, MaxTop = 500)]
    public PagedProduct Search(ODataQueryOptions<Product> queryOptions)
    {
        // create a query for that would count the total rows
        var countQuery = queryOptions.ApplyTo(Repository.Query(),  AllowedQueryOptions.Top | AllowedQueryOptions.Skip) as IQueryable<Product>;

        // create a query to pull the data
        var query = queryOptions.ApplyTo(Repository.Query()) as IQueryable<Product>;

        var result = new PagedProduct()
        {
            CurrentPage = 1, // TODO get the current page value from the queryOptions
            PageSize = 100,  // TODO get the page size value from the queryOptions
        };

        result.TotalRecords = await countQuery.Count();
        result.Data = await query.ToList();

        return result;
    }
}

但是上面的代码导致 Swagger UI 失败。我收到以下错误

Fetch error undefined /swagger/v1/swagger.json

该错误是由使用 ODataQueryOptions 引起的作为搜索操作的参数。

如何让 Swagger UI 与 ODataQueryOptions 一起工作?

最佳答案

几年前我遇到过类似的问题,所以也许我的解决方案对你有用。

我在我的 Controller 中使用“本地”参数而不是 ODataQueryOptions:

    [Route("", Name = "GetDocuments")]
    [HttpGet]
    [Produces(typeof(PageDto<DocumentShortDto>))]
    public async Task<IActionResult> GetDocumentsAsync(
        [FromQuery(Name = "$top")] int top,
        [FromQuery(Name = "$skip")] int skip,
        [FromQuery(Name = "$orderby")] string orderby,
        [FromQuery(Name = "$filter")] string filter)

然后我在以下类的帮助下创建了 ODataQueryOptions:

public static class ODataBuilder
{
    public static ODataQueryOptions<T> BuildOptions<T>(HttpRequest request)
    {
        var modelBuilder = new ODataConventionModelBuilder();
        modelBuilder.AddEntityType(typeof(T));
        var edmModel = modelBuilder.GetEdmModel();
        var oDataQueryContext = new ODataQueryContext(edmModel, typeof(T), new ODataPath());

        return new ODataQueryOptions<T>(oDataQueryContext, request);
    }
}

这种方法的优点是用户现在可以指定所需的参数并在 Swagger 中运行请求。

如果 Swagger 抛出 InvalidOperationException 并显示消息“将至少一种媒体类型添加到支持的媒体类型列表”,这意味着某些 OData 格式化程序具有不受支持的媒体类型.

由于服务仅使用 native 参数并且不响应 OData,您可以安全地删除启动代码中的所有 OData 格式化程序并解决此问题:

services.AddMvcCore(options =>
{
    var inputFormattersToRemove = options.InputFormatters.OfType<ODataInputFormatter>().ToList();
    foreach (var formatter in inputFormattersToRemove)
    {
        options.InputFormatters.Remove(formatter);
    }

    var outputFormattersToRemove = options.OutputFormatters.OfType<ODataOutputFormatter>().ToList();
    foreach (var formatter in outputFormattersToRemove)
    {
        options.OutputFormatters.Remove(formatter);
    }
});

关于c# - 如何在 asp.net 核心 WebApi 项目中使用带有 swagger 的 OData 过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69317761/

相关文章:

C# .NET 4.0 MVC 插入带有主键的记录

asp.net-core - 设置 ASP.NET Core 2.0 图标

angular - 具有多个 Angular 2 模块的 webpack

swagger - 安装 Swashbuckle.AspNetCore 包后 Aspnet core 2.2 默认路由更改为 "~/index.html"

php - API 平台 : Filtering Custom Data Provider

C# 将对象数组添加到 ICollection - 转换 dto -> 模型

c# - 如何设计这个用于SQL搜索的算法?

c# - 在 ASP.NET CORE Web API 中获取客户端 IP 地址有问题吗?

c# - 获取 ASP.NET Core 中发生异常的行号

api - 无法使用从 swagger api 生成的客户端