MrBlue
MrBlue

Reputation: 161

C# web api swagger integration

I have a problem to integrate swagger ui with my web api, and i don't have any idee what is the problem.

When i call in the browser the swagger, the page http://localhost:56381/swagger/ui/index is like in this screenshot

http://localhost:56381/swagger/ui/index

In the SwaggerConfig.cs file i have this code:

[assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")]

 namespace dummyNamespace
 {
    public class SwaggerConfig
    {
       public static void Register()
      {
        var thisAssembly = typeof(SwaggerConfig).Assembly;
        GlobalConfiguration.Configuration
            .EnableSwagger(c => c.SingleApiVersion("v1", "Test API"))
            .EnableSwaggerUi();

       }
    }
  }

I follow this tutorial: http://www.wmpratt.com/swagger-and-asp-net-web-api-part-1/ . But not working.

I don't now what is wrong with my configuratio.

I use .net framework 4.5.2, Web api 2.0 and Swashbuckle.5.5.3 version

Update: When I call this url

http://localhost:56381/swagger/docs/v1

Return this image: docs/v1

Update1:

After i put this code in my WebApiConfig.cs:

var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
        config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);

Now the http://localhost:56381/swagger/ui/index return this json:

  {
    "statusCode": 200
  }

Any idee how to make make http://localhost:56381/swagger/ui/index return this page: enter image description here

enter image description here

The page is from a test project.

Upvotes: 4

Views: 3202

Answers (1)

ToDevAndBeyond
ToDevAndBeyond

Reputation: 1503

After reading the tutorial you posted, it looks like somehow your root URL may be different than what swagger expects. From the swagger config file:

// By default, the service root url is inferred from the request used to access the docs. // However, there may be situations (e.g. proxy and load-balanced environments) where this does not // resolve correctly. You can workaround this by providing your own code to determine the root URL. // //c.RootUrl(req => GetRootUrlFromAppConfig());

If that is not the case, it could be that the swashbuckle nuget install is somehow corrupted. Try removing the nuget package and reinstalling it.

Upvotes: 0

Related Questions