0

I'm using odata controller in asp.net core application. now for documentation, I want to use swagger. I tried with many approaches but nothing is working.

Please suggest what need to do for integrating swagger

Satish Somani
  • 65
  • 1
  • 2
  • 6
  • 1
    This is a known issue, you can track this [here](https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/581), You can also see the workarounds tried by the developers on this link if it can help you. – Vivek Nuna Oct 01 '18 at 09:56
  • I've described what to do, along with some custom code here: https://stackoverflow.com/questions/59853954/using-nswag-with-odata-creates-error-when-try-to-access-swagger-endpoint – Kevat Shah Aug 24 '21 at 14:03

1 Answers1

0

You need to install the API Explorer for Microsoft ASP.NET Core and OData v4.0 and then take a look over some samples and documentation.

Code snippet from the docs:

public void ConfigureServices( IServiceCollection services )
{
  // format the version as "'v'major[.minor][-status]"
  services.AddMvc();
  services.AddVersionedApiExplorer( o => o.GroupNameFormat = "'v'VVV" );
  services.AddApiVersioning();
  services.AddOData().EnableApiVersioning();
  services.AddSwaggerGen(
    options =>
    {
      var provider = services.BuildServiceProvider()
                             .GetRequiredService<IApiVersionDescriptionProvider>();

      foreach ( var description in provider.ApiVersionDescriptions )
      {
        options.SwaggerDoc(
          description.GroupName,
          new Info()
          {
            Title = $"Sample API {description.ApiVersion}",
            Version = description.ApiVersion.ToString()
          } );
      }
   } );
}

public void Configure(
    IApplicationBuilder app,
    VersionedODataModelBuilder modelBuilder,
    IApiVersionDescriptionProvider provider )
{
    var models = modelBuilder.GetEdmModels();
    app.UseMvc( routes => routes.MapVersionedODataRoutes( "odata", null, models ) );
    app.UseSwagger();
    app.UseSwaggerUI(
        options =>
        {
            foreach ( var description in provider.ApiVersionDescriptions )
            {
                options.SwaggerEndpoint(
                    $"/swagger/{description.GroupName}/swagger.json",
                    description.GroupName.ToUpperInvariant() );
            }
        } );
}
Cristi Pufu
  • 9,002
  • 3
  • 37
  • 43