Swagger中隐藏API

在使用Swagger需要隐藏不开放的API
可以在SwaggerConfig.cs中加入

        ///   
        /// 隐藏接口,不生成到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);
                    }
                }
            }
        }

Swagger中隐藏API_第1张图片
并且在SwaggerConfig.cs 中加入

c.DocumentFilter();
Swagger中隐藏API_第2张图片
API调实例

///   
/// 隐藏接口方法生成文档(添加[HiddenApi]特性)  
///   
///   
///   
[HiddenApi]  
[Route("HideApi")]          
[HttpGet]  
public HttpResponseMessage HideApi(string value)  
{  
    return Request.CreateResponse(HttpStatusCode.OK, new { errorCode = "0", value = value });  
}  

你可能感兴趣的:(WEB前端,WEBAPI,WEBAPI)