Swashbuckle.webapi: No API descriptions with Swashbuckle 5.0 and OWIN

Created on 19 Feb 2015  路  7Comments  路  Source: domaindrivendev/Swashbuckle.WebApi

I've read the documentation on Swashbuckle 5.0 and the only thing we have to do is to enable Swagger and SwaggerUI via Swashbuckle.Core. The /swagger/ui/index shows up but with no descriptions...

Here is my Startup.cs :

public partial class Startup
  {
    public void Configuration(IAppBuilder app)
    {
      ConfigureAuth(app);
      ConfigureWebApi(app);
      RegisterSwagger(app);
    }
  }

Here is my Startup.SwaggerConfig.cs :

public partial class Startup
    {
      public static void RegisterSwagger(IAppBuilder app)
      {
          GlobalConfiguration.Configuration 
            .EnableSwagger(c =>
                  {
                      c.SingleApiVersion("v1", "MyAPI");
                      c.IncludeXmlComments(GetXmlCommentsPath());
                  })
              .EnableSwaggerUi();
      }

      protected static string GetXmlCommentsPath()
      {
        return System.String.Format(@"{0}\bin\MyAPI.XML", System.AppDomain.CurrentDomain.BaseDirectory);
      }
    }

Did I miss something?
I've make some research on that issue and it seems that with Swashbuckle 4.0 there was an
Swashbuckle.Bootstrapper.Init(config);
Is it replaced with something else?

Most helpful comment

Ok, I have managed to configure Swagger inside OWIN Startup class:

public void Configuration(IAppBuilder app)
    {
        HttpConfiguration config = new HttpConfiguration();

        // Swagger
        SwaggerConfig.Register(config);

        // Authentication token
        ConfigureOAuth(app);

        // SignalR configuration
        ConfigureSignalR(app);

        // Register routes
        WebApiConfig.Register(config);

        // Allow cross-domain requests
        app.UseCors(CorsOptions.AllowAll);

        app.UseWebApi(config);
    }

And in SwaggerConfig,cs:

using Swashbuckle.Application;
using System.Web.Http;

namespace Name.API 
{
     public class SwaggerConfig
     {
          public static void Register(HttpConfiguration config)
          {
                config.EnableSwagger(c =>
                {
                    c.SingleApiVersion("v1", "Name.API");   
                })
            .EnableSwaggerUi(c =>
                {
                });
         }
     }
}

All 7 comments

I think that I can't use GlobalConfiguration.Configuration to enable Swagger because the WebApi Routes configured with the method ConfigureWebApi() are not present. This is probably why there is no API Descriptions. I tried to enable Swagger in the ConfigureWebApi() method like this:

public void ConfigureWebApi(IAppBuilder app)
    {
      var config = new HttpConfiguration();
      config.SuppressDefaultHostAuthentication();
      config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

      config.EnableSwagger(c =>
      {
        c.SingleApiVersion("v1", "MyAPI");
        c.IncludeXmlComments(System.String.Format(@"{0}\bin\MyAPI.XML", System.AppDomain.CurrentDomain.BaseDirectory));
      })
      .EnableSwaggerUi();

      // Web API routes
      config.MapHttpAttributeRoutes();

      config.Routes.MapHttpRoute(
          name: "DefaultApi",
          routeTemplate: "api/{controller}/{id}",
          defaults: new { id = RouteParameter.Optional }
      );

      app.UseWebApi(config);
    }

But now I get the page shows an error:

image

Any idea?

If you navigate to the discovery URL, you should get a more detailed error

Ok thanks!

Any solution on this matter yet? I have also removed the Global.asax file from the template and added an OWIN Startup class and now Swagger generates empty API description.

Ok, I have managed to configure Swagger inside OWIN Startup class:

public void Configuration(IAppBuilder app)
    {
        HttpConfiguration config = new HttpConfiguration();

        // Swagger
        SwaggerConfig.Register(config);

        // Authentication token
        ConfigureOAuth(app);

        // SignalR configuration
        ConfigureSignalR(app);

        // Register routes
        WebApiConfig.Register(config);

        // Allow cross-domain requests
        app.UseCors(CorsOptions.AllowAll);

        app.UseWebApi(config);
    }

And in SwaggerConfig,cs:

using Swashbuckle.Application;
using System.Web.Http;

namespace Name.API 
{
     public class SwaggerConfig
     {
          public static void Register(HttpConfiguration config)
          {
                config.EnableSwagger(c =>
                {
                    c.SingleApiVersion("v1", "Name.API");   
                })
            .EnableSwaggerUi(c =>
                {
                });
         }
     }
}

Thanks @corneliu-serediuc this handled my problem. The actual nuget package uses [assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")] which only works when the project isn`t based on OWIN. If you comment this attribute out, and call the Register from the Startup.cs the methods get discovered just fine... Thanks!

@corneliu-serediuc Thanks mate, you saved me time investigating this!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

maheshmohandas picture maheshmohandas  路  4Comments

johnpmcclung picture johnpmcclung  路  3Comments

thj-dk picture thj-dk  路  5Comments

kongres picture kongres  路  4Comments

DotNetRockStar picture DotNetRockStar  路  3Comments