如何在运行于ASP.NET Core 3.1的启用OData的Web API中添加Swagger

超级JMN

我想在我的Web API中同时使用OData和Swagger。我正在运行ASP.NET Core 3.1。

我找到了这些文章,其中一篇启用OData,另一篇启用SwaggerUI

但是,我似乎无法同时启用两者。看来我在混错他们。

这是我目前拥有的代码:

启动文件

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();
        services.AddOData();
        AddSwagger(services);
    }

    // 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();
        }

        app.UseHttpsRedirection();
        app.UseRouting();
        app.UseAuthorization();

        app.UseSwagger();
        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "Foo API V1");
        });

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
            endpoints.Select().Filter().OrderBy().Count().MaxTop(10);
            endpoints.MapODataRoute("odata", "odata", GetEdmModel());
        });
    }

    private IEdmModel GetEdmModel()
    {
        var odataBuilder = new ODataConventionModelBuilder();
        odataBuilder.EntitySet<WeatherForecast>("WeatherForecast");

        return odataBuilder.GetEdmModel();
    }

    private void AddSwagger(IServiceCollection services)
    {
        services.AddSwaggerGen(options =>
        {
            var groupName = "v1";

            options.SwaggerDoc(groupName, new OpenApiInfo
            {
                Title = $"Foo {groupName}",
                Version = groupName,
                Description = "Foo API",
                Contact = new OpenApiContact
                {
                    Name = "Foo Company",
                    Email = string.Empty,
                    Url = new Uri("https://example.com/"),
                }
            });
        });
    }
}

当我转到https:// localhost:44363 / odata / weatherforecast时,它可以工作,但是当我尝试加载Swagger接口时,这表明:

在此处输入图片说明

它什么也没显示!

这是我的控制器:

控制者

[Route(“ [controller]”)]公共类WeatherForecastController:ControllerBase {私有静态只读字符串[]摘要= new [] {“冻结”,“拥抱”,“寒冷”,“酷”,“轻度”,“温暖” “,” Balmy“,” Hot“,” Sweltering“,” Scorching“};

    [EnableQuery]
    public IEnumerable<WeatherForecast> Get()
    {
        var rng = new Random();
        return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Id = Guid.NewGuid(),
                Date = DateTime.Now.AddDays(index),
                TemperatureC = rng.Next(-20, 55),
                Summary = Summaries[rng.Next(Summaries.Length)]
            })
            .ToArray();
    }
}
骨肉

我在nuget包下面使用,此问题已解决。安装包OData.Swagger

参考:https : //github.com/KishorNaik/Sol_OData_Swagger_Support

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在ASP.net Core WebAPI中启用CORS

如何在ASP.NET Core中启用CORS

如何在ASP.NET Core中启用ClientCache

如何在ASP.Net 5 Web API中启用OData

模块化ASP.NET Web API:如何在运行时向Web API添加/删除路由

使用ASP.NET Core Web API重命名Swashbuckle 6(Swagger)中的模型

如何在ASP.NET Core Web API中启用BSON序列化?

如何使用异步Task <IActionResult>?或者如何在我的Asp.Net Core Web Api中以异步方式运行

如何在.NET Core Web API的swagger中设置基本路径属性

Cutomize Swagger UI ASP.NET Core Web API

如何在运行时在ASP.Net Core中创建URL重写?

如何在Asp.net Core Web API中为messagepack内容类型启用LZ4压缩

如何在端口5003上运行ASP .NET Core API?

如何在asp.net core 3中添加全局路由前缀?

将Swagger添加到ASP.Net Core Web API

Odata ASP.NET Core 2.2 Web API分页

如何在ASP .NET应用程序上的OData和.NET Core 3.0中启用计数?

如何在oData .Net Core 3.1中启用$ levels

如何在.Net Core OData中添加后处理?

ASP.Net Web Api 如何在运行时更改令牌过期时间

如何在运行时更改 asp.net core 中的启动数据?

创建 swagger 示例模型 asp.net core web api

如何在运行时生成的 asp.net web 表的每一行中动态添加删除和更新按钮

如何正确处理 ASP.Net Core 3 Web API 中的多个端点

如何确定是否在运行时启用端点路由 ASP.Net Core

如何在运行 .net core web 应用程序时解决这个问题

如何在 docker 容器中运行已编译的 SPA 静态文件以及 ASP.NET Core Web API 发布?

如何在 ASP.NET Core Web 应用程序中添加 API

如何在 ASP.NET Core API 中的 Swagger 中将注释显示为描述段落