.net framework webapi添加swagger

Posted Blogger

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了.net framework webapi添加swagger相关的知识,希望对你有一定的参考价值。

1、添加引用

nuget 搜索swagger,安装Swashbuckle   (补充如果是.net core api请安装Sawshbuckle aspnetcore)

 

 2、打开项目App_Start文件夹,修改SwaggerConfig.cs配置文件

 

 3、修改Api说明

 

4、创建项目xml注释文档

右键项目→属性→生成→选中下方的 "XML文档文件" 然后保存

 

 5、配置启用:

 

 

 

c.IncludeXmlComments(XmlCommentsPath);

c.IncludeXmlComments(ModelXmlCommentsPath);

 

 

 

        /// <summary>
        /// XML注释路径
        /// </summary>
        public static string XmlCommentsPath
        {
            get
            {
                string path = string.Format("{0}/bin/ShanXiZhongXing.Web.xml", System.AppDomain.CurrentDomain.BaseDirectory);
                return path;
            }
        }

        /// <summary>
        /// 模型XML注释路径
        /// </summary>
        public static string ModelXmlCommentsPath
        {
            get
            {
                string path = string.Format("{0}/bin/ShanXiZhongXing.Contracts.xml", System.AppDomain.CurrentDomain.BaseDirectory);
                return path;
            }
        }

 

 

 6、启动webapi,然后在接口地址后面输入 /swagger   (默认是英文的,如果需要中文显示  还需要做汉化处理)

 

 

 

汉化:

1、创建一个SwaggerControllerDescProvider类

/// <summary>
    /// swagger显示控制器的描述
    /// </summary>
    public class SwaggerControllerDescProvider : ISwaggerProvider
    {
        private readonly ISwaggerProvider _swaggerProvider;
        private static ConcurrentDictionary<string, SwaggerDocument> _cache = new ConcurrentDictionary<string, SwaggerDocument>();
        private readonly string _xml;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="swaggerProvider"></param>
        /// <param name="xml">xml文档路径</param>
        public SwaggerControllerDescProvider(ISwaggerProvider swaggerProvider, string xml)
        {
            _swaggerProvider = swaggerProvider;
            _xml = xml;
        }

        public SwaggerDocument GetSwagger(string rootUrl, string apiVersion)
        {
            var cacheKey = string.Format("{0}_{1}", rootUrl, apiVersion);
            SwaggerDocument srcDoc = null;
            //只读取一次
            if (!_cache.TryGetValue(cacheKey, out srcDoc))
            {
                srcDoc = _swaggerProvider.GetSwagger(rootUrl, apiVersion);

                srcDoc.vendorExtensions = new Dictionary<string, object> { { "ControllerDesc", GetControllerDesc() } };
                _cache.TryAdd(cacheKey, srcDoc);
            }
            return srcDoc;
        }

        /// <summary>
        /// 从API文档中读取控制器描述
        /// </summary>
        /// <returns>所有控制器描述</returns>
        public ConcurrentDictionary<string, string> GetControllerDesc()
        {
            string xmlpath = _xml;
            ConcurrentDictionary<string, string> controllerDescDict = new ConcurrentDictionary<string, string>();
            if (File.Exists(xmlpath))
            {
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.Load(xmlpath);
                string type = string.Empty, path = string.Empty, controllerName = string.Empty;

                string[] arrPath;
                int length = -1, cCount = "Controller".Length;
                XmlNode summaryNode = null;
                foreach (XmlNode node in xmldoc.SelectNodes("//member"))
                {
                    type = node.Attributes["name"].Value;
                    if (type.StartsWith("T:"))
                    {
                        //控制器
                        arrPath = type.Split(\'.\');
                        length = arrPath.Length;
                        controllerName = arrPath[length - 1];
                        if (controllerName.EndsWith("Controller"))
                        {
                            //获取控制器注释
                            summaryNode = node.SelectSingleNode("summary");
                            string key = controllerName.Remove(controllerName.Length - cCount, cCount);
                            if (summaryNode != null && !string.IsNullOrEmpty(summaryNode.InnerText) && !controllerDescDict.ContainsKey(key))
                            {
                                controllerDescDict.TryAdd(key, summaryNode.InnerText.Trim());
                            }
                        }
                    }
                }
            }
            return controllerDescDict;
        }
    }

 

2、创建汉化swagger_lang.js文件

 

 

 

重点:添加进去的js文件必须改为嵌入的资源,否则会导致汉化不起作用

 添加Swagger.Net和Swagger.Net.UI 程序nuget引用

注释SwaggerNet头两行assembly

 

 

 

 

 

 

 

 

 

源文:

https://www.cnblogs.com/yhnet/p/12155412.html net framework webapi添加swagger
https://www.cnblogs.com/dawenyang/p/10911051.html webapi 集成swagger

以上是关于.net framework webapi添加swagger的主要内容,如果未能解决你的问题,请参考以下文章

从 .NET Core 调用 Web API 到 .NET Framework

IIs6基础上发布WebApi注意事项

基于 .NET Framework 的 .NET Core 2.1 Web API 的授权问题

.net webapi 返回类型

如何在Windows Server2008R2中部署WebAPI

发布在 C# .net framework 4.6 中开发的 Web API 的命令 [重复]