• webapi自动生成帮助文档,如何设置加载多个xml文件


    咱们新建webapi项目时,默认会生成HelpPage,里面会有api的详细说明,但默认类只能加载当前项目的xml文件,其他项目中的字段说明就没法显示了,如何解决?往下看

    1. 咱们首先新建一个支持加载多xml的类 MultiXmlDocumentationProvider

    /// <summary>A custom <see cref='IDocumentationProvider'/> that reads the API documentation from a collection of XML documentation files.</summary>
        public class MultiXmlDocumentationProvider : IDocumentationProvider, IModelDocumentationProvider
        {
            /*********
            ** Properties
            *********/
            /// <summary>The internal documentation providers for specific files.</summary>
            private readonly XmlDocumentationProvider[] Providers;
     
     
            /*********
            ** Public methods
            *********/
            /// <summary>Construct an instance.</summary>
            /// <param name='paths'>The physical paths to the XML documents.</param>
            public MultiXmlDocumentationProvider(params string[] paths)
            {
                this.Providers = paths.Select(p => new XmlDocumentationProvider(p)).ToArray();
            }
     
            /// <summary>Gets the documentation for a subject.</summary>
            /// <param name='subject'>The subject to document.</param>
            public string GetDocumentation(MemberInfo subject)
            {
                return this.GetFirstMatch(p => p.GetDocumentation(subject));
            }
     
            /// <summary>Gets the documentation for a subject.</summary>
            /// <param name='subject'>The subject to document.</param>
            public string GetDocumentation(Type subject)
            {
                return this.GetFirstMatch(p => p.GetDocumentation(subject));
            }
     
            /// <summary>Gets the documentation for a subject.</summary>
            /// <param name='subject'>The subject to document.</param>
            public string GetDocumentation(HttpControllerDescriptor subject)
            {
                return this.GetFirstMatch(p => p.GetDocumentation(subject));
            }
     
            /// <summary>Gets the documentation for a subject.</summary>
            /// <param name='subject'>The subject to document.</param>
            public string GetDocumentation(HttpActionDescriptor subject)
            {
                return this.GetFirstMatch(p => p.GetDocumentation(subject));
            }
     
            /// <summary>Gets the documentation for a subject.</summary>
            /// <param name='subject'>The subject to document.</param>
            public string GetDocumentation(HttpParameterDescriptor subject)
            {
                return this.GetFirstMatch(p => p.GetDocumentation(subject));
            }
     
            /// <summary>Gets the documentation for a subject.</summary>
            /// <param name='subject'>The subject to document.</param>
            public string GetResponseDocumentation(HttpActionDescriptor subject)
            {
                return this.GetFirstMatch(p => p.GetDocumentation(subject));
            }
     
     
            /*********
            ** Private methods
            *********/
            /// <summary>Get the first valid result from the collection of XML documentation providers.</summary>
            /// <param name='expr'>The method to invoke.</param>
            private string GetFirstMatch(Func<XmlDocumentationProvider, string> expr)
            {
                return this.Providers
                    .Select(expr)
                    .FirstOrDefault(p => !String.IsNullOrWhiteSpace(p));
            }
        }

    2.找到HelpPageConfig,vs可以搜索到

    在Register方法中添加如下代码

    config.SetDocumentationProvider(new MultiXmlDocumentationProvider(
                    HttpContext.Current.Server.MapPath("~/App_Data/ZhongXin.ZXWX.WebApi.xml"),
                    HttpContext.Current.Server.MapPath("~/App_Data/ZhongXin.ZXWX.Model.xml"),
                    HttpContext.Current.Server.MapPath("~/App_Data/ZhongXin.ZXWX.Common.xml")
                    ));

    OK,运行起来吧

    注意事项:xml文件要看看是否生成了

    .网上有说要在HelpPageConfig中注释掉一下代码,试过没注释也没问题

    #region *************************默认JSON显示接口***************************************
                //config.SetSampleForMediaType(
                //    new TextSample("Binary JSON content. See http://bsonspec.org for details."),
                //    new MediaTypeHeaderValue("application/bson"));
                #endregion

    另外也可以使用swagger生成API文档,可在Nuget下载

  • 相关阅读:
    C# winfrom容器布局与工具栏&&右键菜单栏&&隐藏显示小图标的的简单事件
    C# Winform ListView控件
    MongoDB3.6.3 windows安装配置、启动
    史蒂夫•乔布斯在斯坦福大学的演讲
    SpringBoot配置文件 application.properties详解
    Elasticsearch分布式安装启动失败
    Couldn't connect to host, port: smtp.163.com, 25; timeout -1;
    CentOS 7 安装jdk9
    生成唯一的随机数(时间+随机数)
    idea 设置背景图片
  • 原文地址:https://www.cnblogs.com/CuiRicky/p/11977983.html
Copyright © 2020-2023  润新知