using System.Web.Http;
using WebActivatorEx;
using Swashbuckle.Application;
using CCFlow.DataUser.API;
using Swashbuckle.Swagger;
using System.Web.Http.Description;
using System.Linq;
[assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")]
namespace CCFlow.DataUser.API
{
///
/// 隐藏接口,不生成到swagger文档展示
///
[System.AttributeUsage(System.AttributeTargets.Method | System.AttributeTargets.Class)]
public partial class HiddenApiAttribute : System.Attribute { }
public class HiddenApiFilter : IDocumentFilter
{
///
/// 重写Apply方法,移除隐藏接口的生成
///
/// swagger文档文件
///
/// api接口集合
public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
{
foreach (ApiDescription apiDescription in apiExplorer.ApiDescriptions)
{
if (Enumerable.OfType(apiDescription.GetControllerAndActionAttributes()).Any())
{
string key = "/" + apiDescription.RelativePath;
if (key.Contains("?"))
{
int idx = key.IndexOf("?", System.StringComparison.Ordinal);
key = key.Substring(0, idx);
}
swaggerDoc.paths.Remove(key);
}
}
}
}
public class SwaggerConfig
{
public static void Register()
{
var thisAssembly = typeof(SwaggerConfig).Assembly;
GlobalConfiguration.Configuration
.EnableSwagger(c =>
{
c.SingleApiVersion("v1", "ToolKit");
string path = System.AppDomain.CurrentDomain.BaseDirectory;
c.IncludeXmlComments(System.String.Format(@"{0}\bin\ToolKit.XML", path));
c.DocumentFilter();
})
.EnableSwaggerUi(c =>{});
}
}
}