AddRestfulSwagger Method
Definition
- Namespace
- Codebelt.Extensions.Swashbuckle.AspNetCore
- Assemblies
- Codebelt.Extensions.Swashbuckle.AspNetCore.dll
AddRestfulSwagger(IServiceCollection, Action<RestfulSwaggerOptions>)
Adds complementary configurations for both SwaggerGenOptions and SwaggerUIOptions - optimized for RESTful APIs.
public static IServiceCollection AddRestfulSwagger(this IServiceCollection services, Action<RestfulSwaggerOptions> setup = null)
Parameters
servicesIServiceCollectionThe IServiceCollection to extend.
setupAction<RestfulSwaggerOptions>The RestfulSwaggerOptions that may be configured.
Returns
- IServiceCollection
A reference to
servicesso that additional calls can be chained.
Examples
Configure Swagger with custom settings through the RestfulSwaggerOptions parameter. This overload accepts a setup action where you can customize API metadata, enable XML documentation loading, and configure advanced SwaggerGen behavior. The example shows how to set document title and version, enable controller XML comments, add API-wide documentation, and use the all-of schema extension for better reference-type composition:
using System;
using Codebelt.Extensions.Swashbuckle.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
namespace MySwaggerExample;
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddRestfulSwagger(options =>
{
options.OpenApiInfo.Title = "Custom API";
options.OpenApiInfo.Description = "Customized API documentation";
options.IncludeControllerXmlComments = true;
options.XmlDocumentations.AddByType<Program>();
options.Settings.UseAllOfToExtendReferenceSchemas();
});
var app = builder.Build();
if (string.Equals(app.Environment.EnvironmentName, "Development", StringComparison.OrdinalIgnoreCase))
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.MapControllers();
app.Run();
}
}
Remarks
This method expect a call to services.AddRestfulApiVersioning() prior to this.