Skip to content

Instantly share code, notes, and snippets.

@Legends
Last active October 13, 2019 18:46
Show Gist options
  • Select an option

  • Save Legends/491210f92c8b61588ae7ee6f827df975 to your computer and use it in GitHub Desktop.

Select an option

Save Legends/491210f92c8b61588ae7ee6f827df975 to your computer and use it in GitHub Desktop.
Swagger UI setup
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace WebApi_SwaggerUI
{
/// <summary>
/// install-package nswag.aspnetcore
/// Run web api and access swagger ui : http://localhost:65384/swagger
/// </summary>
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddSwaggerDocument(config =>
{
config.PostProcess = document =>
{
document.Info.Version = "v1";
document.Info.Title = "ToDo API";
document.Info.Description = "A simple ASP.NET Core web API";
document.Info.TermsOfService = "None";
document.Info.Contact = new NSwag.OpenApiContact
{
Name = "jm",
Email = string.Empty,
Url = "https://twitter.com/jm"
};
document.Info.License = new NSwag.OpenApiLicense
{
Name = "Use under LICX",
Url = "https://example.com/license"
};
};
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
#region swagger
// URLs:
// - http://localhost:65384/swagger/v1/swagger.json
// - http://localhost:65384/swagger
// - http://localhost:65384/redoc
// - http://localhost:65384/openapi
// - http://localhost:65384/openapi_redoc
app.UseOpenApi();
app.UseSwaggerUi3();
app.UseReDoc();
#endregion//// Add OpenAPI and Swagger middlewares to serve documents and web UIs
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment