Last active
July 19, 2017 13:06
-
-
Save EifelMono/14661357d60a01b0b5992996758da411 to your computer and use it in GitHub Desktop.
Swagger (Swashbuckle) Startup in Asp.Net Core with Xml documentation!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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