// Program.cs
builder.Services.AddControllers().AddJsonOptions(opt =>
{
opt.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
});
Automatically / Dynamically create Swagger documentation for all API Endpoints
// Generate Documentation for each endpoint automatically
var actionDescriptors = actionDescriptor.ActionDescriptors.Items.ToList();
actionDescriptors.ForEach(x =>
{
IFilterMetadata noContent = new ProducesResponseTypeAttribute(204);
x.FilterDescriptors.Add(new FilterDescriptor(noContent, 0));
IFilterMetadata unauth = new ProducesResponseTypeAttribute(401);
x.FilterDescriptors.Add(new FilterDescriptor(unauth, 0));
IFilterMetadata serverError = new ProducesResponseTypeAttribute(500);
x.FilterDescriptors.Add(new FilterDescriptor(serverError, 0));
});
Startup.cs example for .NET Core API w/ Swagger
using Swashbuckle.AspNetCore.Swagger;
public void ConfigureServices(IServiceCollection services)
{
services.AddMvcCore().AddApiExplorer();
services.AddSwaggerGen();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
});
services.AddTransient<ISomeInterface, SomeDependency>();
}
// -----------------
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API");
});
}
app.UseMvc();
}