Asp.net core mvc 如何使用Swashback.AspNetCore.Swagger忽略/更改asp.net core中的模型参数

Asp.net core mvc 如何使用Swashback.AspNetCore.Swagger忽略/更改asp.net core中的模型参数,asp.net-core-mvc,swagger,odata,swashbuckle,swashbuckle.aspnetcore,Asp.net Core Mvc,Swagger,Odata,Swashbuckle,Swashbuckle.aspnetcore,我的项目引用了以下包 Swashback.AspNetCore.Filters v6.0.0 Swashback.AspNetCore.Swagger v5.6.3 Swashback.AspNetCore.SwagggerGen v5.6.3 Swashback.AspNetCore.SwaggerNewtonSoft v5.6.3 Microsoft.AspNetCore.OData v7.5.0 问题是: 我有一个名为“TestController”的控制器。其中有一个名为Test的[Ht

我的项目引用了以下包

  • Swashback.AspNetCore.Filters v6.0.0
  • Swashback.AspNetCore.Swagger v5.6.3
  • Swashback.AspNetCore.SwagggerGen v5.6.3
  • Swashback.AspNetCore.SwaggerNewtonSoft v5.6.3
  • Microsoft.AspNetCore.OData v7.5.0
  • 问题是:

    我有一个名为“TestController”的控制器。其中有一个名为Test的[HttpGet]方法

    该方法装饰如下

    [HttpGet]
    [招摇过市操作(操作ID=nameof(测试))]
    公共IActionResult测试([FromQuery]字符串id,[FromQuery]ODataQueryOptions oData)
    {
    // ...
    }
    
    因为我使用的是Swashback,所以预期的结果应该是有一个名为Test的get方法,其中包含一组返回到文档UI的查询参数

    然而,我看到了一个例外。例外情况是:

    Failed to generate Scheme for type - ODataQueryOptions<`T>. See inner exception
    
    什么都没用。同样的例外

    在这一点上,我的问题相当简单

    如何从swagger文档生成中删除ODataQueryOption参数

    编辑:添加异常消息

    • 无法为类型-Microsoft.AspNet.OData.Query.ODataQueryOptions`1[SearchOptions]生成架构。 参见内部异常
    • 未能为类型生成架构- Microsoft.AspNetCore.Http.HttpRequest。参见内部异常
    • 未能为类型生成架构- Microsoft.AspNetCore.Http.HttpContext。参见内部异常
    • 未能为类型生成架构- Microsoft.AspNetCore.Http.Authentication.AuthenticationManager。看见 内部异常
    • 无法加载类型 'Microsoft.AspNetCore.Http.Features.Authentication.AuthenticateContext' 从程序集“Microsoft.AspNetCore.Http.Features,Version=3.1.8.0, 区域性=中性,PublicKeyToken=adb9793829ddae60'

    它可以在我的项目中很好地工作:

    操作(确保在
    ODataQueryOptions
    上删除
    [FromQuery]
    ):

    [HttpGet]
    [启用查询]
    公共IActionResult测试获取([FromQuery]字符串id,ODataQueryOptions,ODataQueryOptions)
    {
    //...
    }
    
    Startup.cs:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddOData();
        services.AddControllers();
    
        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
        });
    
        SetOutputFormatters(services);
    }
    
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
    
        app.UseHttpsRedirection();
    
        app.UseRouting();
    
        app.UseAuthorization();
    
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
            endpoints.EnableDependencyInjection();
            endpoints.Select().Filter().Expand().MaxTop(10);
            endpoints.MapODataRoute("odata", "odata", GetEdmModel());
        });
    
        app.UseSwagger();
    
        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
        });
    }
    
    IEdmModel GetEdmModel()
    {
        var builder = new ODataConventionModelBuilder();
        builder.EntitySet<WeatherForecast>("WeatherForecast");
        return builder.GetEdmModel();
    }
    
    private static void SetOutputFormatters(IServiceCollection services)
    {
        services.AddMvcCore(options =>
        {
            IEnumerable<ODataOutputFormatter> outputFormatters =
                options.OutputFormatters.OfType<ODataOutputFormatter>()
                    .Where(foramtter => foramtter.SupportedMediaTypes.Count == 0);
    
            foreach (var outputFormatter in outputFormatters)
            {
                outputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/odata"));
            }
        });
    }
    
    public void配置服务(IServiceCollection服务)
    {
    services.AddOData();
    services.AddControllers();
    services.AddSwaggerGen(c=>
    {
    c、 SwaggerDoc(“v1”,新的openapinfo{Title=“myapi”,Version=“v1”});
    });
    SetOutputFormatters(服务);
    }
    public void配置(IApplicationBuilder应用程序、IWebHostEnvironment环境)
    {
    if(env.IsDevelopment())
    {
    app.UseDeveloperExceptionPage();
    }
    app.UseHttpsRedirection();
    app.UseRouting();
    app.UseAuthorization();
    app.UseEndpoints(端点=>
    {
    endpoints.MapControllers();
    endpoints.EnableDependencyInjection();
    endpoints.Select().Filter().Expand().MaxTop(10);
    MapODataRoute(“odata”,“odata”,GetEdmModel());
    });
    app.UseSwagger();
    app.UseSwaggerUI(c=>
    {
    c、 SwaggerEndpoint(“/swagger/v1/swagger.json”,“我的API v1”);
    });
    }
    IEdmModel GetEdmModel()
    {
    var builder=新的ODataConventionModelBuilder();
    建造商实体集(“天气预报”);
    返回builder.GetEdmModel();
    }
    专用静态void SetOutputFormatters(IServiceCollection服务)
    {
    services.AddMvcCore(选项=>
    {
    IEnumerable输出格式化程序=
    options.OutputFormatters.OfType()的
    .其中(FORAMETER=>FORAMETER.SupportedMediaTypes.Count==0);
    foreach(outputFormatters中的var outputFormatter)
    {
    outputFormatter.SupportedMediaTypes.Add(新的MediaTypeHeaderValue(“应用程序/odata”);
    }
    });
    }
    
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddOData();
        services.AddControllers();
    
        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
        });
    
        SetOutputFormatters(services);
    }
    
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
    
        app.UseHttpsRedirection();
    
        app.UseRouting();
    
        app.UseAuthorization();
    
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
            endpoints.EnableDependencyInjection();
            endpoints.Select().Filter().Expand().MaxTop(10);
            endpoints.MapODataRoute("odata", "odata", GetEdmModel());
        });
    
        app.UseSwagger();
    
        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
        });
    }
    
    IEdmModel GetEdmModel()
    {
        var builder = new ODataConventionModelBuilder();
        builder.EntitySet<WeatherForecast>("WeatherForecast");
        return builder.GetEdmModel();
    }
    
    private static void SetOutputFormatters(IServiceCollection services)
    {
        services.AddMvcCore(options =>
        {
            IEnumerable<ODataOutputFormatter> outputFormatters =
                options.OutputFormatters.OfType<ODataOutputFormatter>()
                    .Where(foramtter => foramtter.SupportedMediaTypes.Count == 0);
    
            foreach (var outputFormatter in outputFormatters)
            {
                outputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/odata"));
            }
        });
    }