Reputation: 294
I have this default Web APi scaffolding . i included following Nuget packages. i have the following file in Startup.cs. however it is throwing error. please assist
My Startup.cs class has the following:-
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.RoutePrefix = "v1";
var basePath = string.IsNullOrWhiteSpace(c.RoutePrefix) ? "." : "..";
c.SwaggerEndpoint($"{basePath}/swagger/{c.RoutePrefix}/swagger.json", "Name");
});
Upvotes: 0
Views: 3387
Reputation: 1614
The reason why you are having this problem is because you are not calling services.AddSwaggerGen()
in your ConfigureServices(IServiceCollection services)
method.
This method registers the ISwaggerProvider
interface in the DI container , since it isn't registered you are receiving this exception when invoking your swagger route.
Full example of swagger section in ConfigureServices
:
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
});
Upvotes: 1