.Net Core 3.1 - Swashbuckler - How can I use appsettings in AddSwaggerGen

396 views Asked by At

I'm having problems trying to use my appsettings in the AddSwaggerGen extension for a parameter for an OperationFilter from Swashbuckler.

I have tried using ServiceProvider, IConfiguration, and preloaded DI class defined earlier in the startup.cs.

Here is one attempt of what I have tried to do:

c.OperationFilter<SwaggerOperationFilter>(
    this.Configuration.GetSection("ApiSettings")["MaxResults"]
);

Console.WriteLine(JsonSerializer.Serialize(this.Configuration.GetSection("ApiSettings")) 
>>> {"Key":"ApiSettings","Path":"ApiSettings","Value":null}

I can swap out this.Configuration.GetSection("ApiSettings")["MaxResults"] with hardcoded value like "100" and it will work correctly. But I need to derive this value from the appsettings, instead of hardcoding it.

I have tried many different ways, thinking that it should work, but it hasn't. The value I get back is null/0 for value, meaning that the object was empty/new. And it is actually not getting the appsettings like it should.

0

There are 0 answers