What is the root cause of the API definition loading issue?
The default API controller route configuration is the cause of this issue. When we add the API controller in the ASP.NET Core API application, by default it has controller-specific routes, which means it can support only a single method for each of the HTTP verbs Post, PUT, Delete, GET, and Patch.
There may be a requirement when we need to create more than one method having the Http verbs Post, PUT, Delete, GET, and Patch in a single API controller, and if you create the method with the default route configuration in the API controller class without modifying the default route configuration, then you will get the exception "Failed to load API definition" while loading the swagger UI in the ASP.NET Core web API.
What is the Solution?
The solution is that you have to change the default route configuration at the controller level when you have created more than one method with the HTTP verbs "post," "put," and "get." Delete or Put in the single API controller class.
Consider the following example: I have created the ASP.NET Core API application, which has by default one Get method, GetWeatherForecast, which returns the WeatherForecast. Then I have added one more Get method named WeatherForecastByCity in the default API Controller class without modifying the default route.
Now change the default route at controller level which can support more than one method having the Http verbs Post, PUT, Delete, GET, and Patch in a single controller.