简体   繁体   English

如何为 Asp.net 核心 OData api 启用 swagger

[英]How enable swagger for Asp.net core OData api

I have an asp.net core odata api.我有一个 asp.net 核心 odata api。 I want to enable swagger for this api.我想为这个 api 启用 swagger。 The version of asp.net core is 2.2 and the dependencies of this project are as below picture: asp.net core的版本是2.2,这个项目的依赖如下图:

在此处输入图片说明

and the config of startup is as below:启动配置如下:

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.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        services.AddDbContext<SeeMiddleContext>(options =>
        {
            options.UseSqlServer(
                "Data Source = 192.168.1.1;Initial Catalog=Seem;persist security info=True;user id=Sas;password=Sas1");
        });
        services.AddOData();

        services.AddSwaggerGen(options =>
        {
            options.DescribeAllEnumsAsStrings();
            options.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info { Title = "eShopOnContainers - Catalog HTTP API", Version = "v1", Description = "The Catalog Microservice HTTP API. This is a DataDriven/CRUD microservice sample", TermsOfService = "Terms Of Service" });
        });
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseMvc(routeBuilder => {

            routeBuilder.EnableDependencyInjection();

            routeBuilder.Expand().Select().OrderBy().Filter().MaxTop(40);
        });
        app.UseSwagger();

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

When I run the api and when I type "/swagger" in the url, then I will encounter the error that is shown in below picture:当我运行 api 并在 url 中输入“/swagger”时,我会遇到如下图所示的错误:

在此处输入图片说明

Try to add the following code in your ConfigureServices method尝试在您的 ConfigureServices 方法中添加以下代码

 using Microsoft.AspNet.OData.Formatter;
 using Microsoft.Net.Http.Headers;

 public void ConfigureServices(IServiceCollection services)
 {
        // Workaround: https://github.com/OData/WebApi/issues/1177
        services.AddMvcCore(options =>
        {
            foreach (var outputFormatter in options.OutputFormatters.OfType<ODataOutputFormatter>().Where(_ => _.SupportedMediaTypes.Count == 0))
            {
                outputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
            }
            foreach (var inputFormatter in options.InputFormatters.OfType<ODataInputFormatter>().Where(_ => _.SupportedMediaTypes.Count == 0))
            {
                inputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
            }
        });
  }

For more details , you could refer to https://stackoverflow.com/a/51599466/10201850有关更多详细信息,您可以参考https://stackoverflow.com/a/51599466/10201850

For me, after all actions above, and described in the post , I still didn't have the odata-endpoint in swagger-ui page.对我来说,在完成上述所有操作并在帖子中描述之后,我仍然没有 swagger-ui 页面中的 odata-endpoint。 The decorator装饰者

[ApiExplorerSettings(IgnoreApi = false)]

for my controller resolved the issue我的控制器解决了这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM