Links
Installation
|
dotnet add package NSwag.AspNetCore
|
Configuration Web API
Program.cs
|
builder.Services.AddSwaggerDocument(configuration =>
{
configuration.Title = "MyApp";
configuration.Version = typeof(Program).Assembly.GetSimplifiedVersion();
});
if (app.Environment.IsDevelopment())
{
app.UseOpenApi();
app.UseSwaggerUi3();
}
|
Configuration MVC
Startup.cs
|
using NJsonSchema;
using NSwag.AspNetCore;
using System.Reflection;
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseMvc();
app.UseSwaggerUi(typeof(Startup).GetTypeInfo().Assembly, settings =>
{
settings.GeneratorSettings.DefaultPropertyNameHandling = PropertyNameHandling.CamelCase;
settings.PostProcess = document =>
{
document.Info.Title = "Test API";
document.Info.Description = "A simple ASP.NET Core web API";
document.Info.Contact = new NSwag.SwaggerContact
{
Name = "Nicolas",
};
};
});
|
Problème avec IActionResult
NSwag utilise la réflexion pour obtenir le type de retour. Avec IActionResult il ne peut pas.
|
[HttpGet]
[SwaggerResponse(HttpStatusCode.OK, typeof(IReadOnlyList<ItemDto>))]
[SwaggerResponse(HttpStatusCode.BadRequest, typeof(void))]
[ProducesResponseType(typeof(IReadOnlyList<ItemDto>), StatusCodes.Status200OK)]
public IActionResult Get()
|