• 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下载

  • 相关阅读:
    github 代理加速
    centos系统语言设置为中文
    红帽 / CentOS安装Jenkins
    查看api有没有更新到位
    永久关闭Windows10或Windows11的系统自动更新
    api传文件连接超时
    docker日常使用
    开发者工具批量替换
    Linux常用工具安装
    office密钥
  • 原文地址:https://www.cnblogs.com/CuiRicky/p/11977983.html
Copyright © 2020-2023  润新知