Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save EifelMono/14661357d60a01b0b5992996758da411 to your computer and use it in GitHub Desktop.
Save EifelMono/14661357d60a01b0b5992996758da411 to your computer and use it in GitHub Desktop.
Swagger (Swashbuckle) Startup in Asp.Net Core with Xml documentation!
// http://x.x.x.x:port/swagger/ui/index.html
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddSwaggerGen();
services.ConfigureSwaggerGen(options =>
{
options.SingleApiVersion(new Info
{
Version = "v1",
Title = "Test Title",
Description = "Test Description",
TermsOfService = "None",
Contact = new Contact
{
Name = "Andreas Klapperich",
Email = "[email protected]",
Url = "http://klapperich.de"
},
License = new License
{
Name = "Use under LICX",
Url = "http://klapperich.de"
}
});
// Only the current project document xml file
//{
// var xmlPath = Path.ChangeExtension(Environment.GetCommandLineArgs()[0], ".xml");
// options.IncludeXmlComments(xmlPath);
//}
{
// Include all Xml's
var xmlPathBase = Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]);
foreach (var filename in Directory.GetFiles(xmlPathBase, "*.xml"))
{
try
{
// Test if it contains an controller!!!!
options.IncludeXmlComments(filename);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
});
// Add framework services.
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseSwagger();
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseMvc();
app.UseSwaggerUi();
DefaultFilesOptions DefaultFile = new DefaultFilesOptions();
DefaultFile.DefaultFileNames.Clear();
DefaultFile.DefaultFileNames.Add("swagger/ui/index.html");
app.UseDefaultFiles(DefaultFile);
app.UseStaticFiles();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment