Last active
June 26, 2019 15:45
-
-
Save seangwright/7699f89e3ce7a637bdd8e7e912a30e04 to your computer and use it in GitHub Desktop.
WebApi2 Swashbuckle integration
This file contains 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
<!-- Add this to ensure the XML comments are picked up by the Swashbuckle integration --> | |
<PropertyGroup> | |
<DocumentationFile>bin\KenticoMvcApp.xml</DocumentationFile> | |
</PropertyGroup> |
This file contains 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
public static class SwaggerNetConfig | |
{ | |
/// <summary> | |
/// Initializes Swagger and SwaggerUI in the application | |
/// </summary> | |
/// <param name="container"></param> | |
/// <returns></returns> | |
public static void ConfigureSwaggerNet(IContainer container) | |
{ | |
var httpConfiguration = container.Resolve<HttpConfiguration>(); | |
httpConfiguration | |
.EnableSwagger(c => ConfigureSwagger(c)) | |
.EnableSwaggerUi(c => ConfigureSwaggerUI(c)); | |
} | |
/// <summary> | |
/// Configures Swagger and how it integrates into the Http Server | |
/// </summary> | |
/// <param name="docsConfig"></param> | |
private static void ConfigureSwagger(SwaggerDocsConfig docsConfig) | |
{ | |
var assemblyName = Assembly.GetExecutingAssembly().GetName(); | |
docsConfig.SingleApiVersion(assemblyName.Version.ToString().Replace('.', '_'), $"{assemblyName.Name}.API"); | |
docsConfig.IncludeXmlComments(HostingEnvironment.MapPath($"~/bin/{assemblyName.Name}.xml")); | |
docsConfig.UseFullTypeNameInSchemaIds(); | |
// This line is specifically for running this api in a virtual directory | |
// See https://github.com/domaindrivendev/Swashbuckle/issues/305 | |
docsConfig.RootUrl(req => req.RequestUri.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute("~/").TrimEnd('/')); | |
} | |
/// <summary> | |
/// Configures the UI elements / front end functionality of swagger | |
/// </summary> | |
/// <param name="uiConfig"></param> | |
private static void ConfigureSwaggerUI(SwaggerUiConfig uiConfig) | |
{ | |
uiConfig.DocExpansion(DocExpansion.None); | |
uiConfig.DisableValidator(); | |
uiConfig.EnableDiscoveryUrlSelector(); | |
} | |
} | |
/// <summary> | |
/// Filters out Kentico's ApiControllers | |
/// </summary> | |
public class FilteredApiExplorer : IApiExplorer | |
{ | |
private readonly IApiExplorer apiExplorer; | |
public Collection<ApiDescription> ApiDescriptions | |
{ | |
get | |
{ | |
string assemblyName = Assembly.GetExecutingAssembly().GetName().Name; | |
return new Collection<ApiDescription>( | |
apiExplorer | |
.ApiDescriptions | |
.Where(d => d.ActionDescriptor.ControllerDescriptor.ControllerType.Namespace.StartsWith(assemblyName)) | |
.ToList()); | |
} | |
} | |
public FilteredApiExplorer(HttpConfiguration httpConfiguration) | |
{ | |
Guard.Against.Null(httpConfiguration, nameof(httpConfiguration)); | |
apiExplorer = httpConfiguration.Services.GetApiExplorer(); | |
} | |
} |
This file contains 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
public static class WebApiConfig | |
{ | |
public static HttpConfiguration ConfigureWebApi(IContainer container) | |
{ | |
// ... | |
config.Services.Replace(typeof(IApiExplorer), container.Resolve<FilteredApiExplorer>()); | |
// ... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment