Failed to load API definition error in Swagger for ASP.NET Core

1 minute read

There are various reasons cause the swagger to fail to load the API definition and you will get the error shared in the below screenshot. Below are the possible reasons,

  1. Swagger misconfiguration
  2. Controller methods without HTTP verbs (HttpGet, HttpPost etc.)
  3. Same class name etc…
  Failed to load definition error in Swagger

Fig 1. Failed to load definition error in Swagger

Solution:

The Swagger can be launched by adding the ‘/swagger’ after your API URL which doesn’t show the error details causing the Swagger to fail to load.
http://localhost:{PortNo}/swagger/index.html

If you navigate to ‘swagger/v1/swagger.json’ page you will see error details which helpful in addressing the issues.
http://localhost:{PortNo}/swagger/v{VersionNo}/swagger.json
Note: v1 is the version of your API. Change the version according to your API version.

Once all the issues are addressed then you can launch the Swagger URL to view the API definitions.

Leave a comment