publicvoidConfigureServices(IServiceCollectionservices)
{
services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", newOpenApiInfo { Title = "EFWebApi", Version = "v1" });
});
}
publicvoidConfigure(IApplicationBuilderapp, IWebHostEnvironmentenv)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "EFWebApi v1");
c.RoutePrefix = string.Empty; // serve the Swagger UI at the app's root (http://localhost:<port>/)
});
}
Usage
Controllers/ItemController.cs
[ApiController]
[Produces("application/json")] // set the Media type
[Route("[controller]")]
publicclassItemController : ControllerBase
{
[HttpGet]
[ProducesResponseType(typeof(IEnumerable<Item>), StatusCodes.Status200OK)] // set the status code and the return typepublicIActionResultGet() { /* ... */ }
}