How can Xml Documentation for Web Api include documentation from beyond the main project?

前端 未结 5 1018
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-28 00:41

The documentation for enabling XmlDoc integration into your Web Api projects appears to only handle situations where all of your API types are part of your WebApi project.

5条回答
  •  自闭症患者
    2020-11-28 01:40

    I ran into this too, but I didn't want to edit or duplicate any of the generated code to avoid problems later.

    Building on the other answers, here's a self-contained documentation provider for multiple XML sources. Just drop this into your project:

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

    ...and enable it in your HelpPageConfig with the paths to the XML documents you want:

    config.SetDocumentationProvider(new MultiXmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/App_Data/Api.xml"), HttpContext.Current.Server.MapPath("~/App_Data/Api.Models.xml")));
    

提交回复
热议问题