Web Api Help Page XML comments from more than 1 files

前端 未结 4 1052
孤街浪徒
孤街浪徒 2020-12-13 06:57

I have different plugins in my Web api project with their own XML docs, and have one centralized Help page, but the problem is that Web Api\'s default Help Page only support

4条回答
  •  情书的邮戳
    2020-12-13 07:32

    I agree with gurra777 that creating a new class is a safer upgrade path. I started with that solution but it involves a fair amount of copy/pasta, which could easily get out of date after a few package updates.

    Instead, I am keeping a collection of XmlDocumentationProvider children. For each of the implementation methods, I'm calling into the children to grab the first non-empty result.

    public class MultiXmlDocumentationProvider : IDocumentationProvider, IModelDocumentationProvider
    {
        private IList _documentationProviders;
    
        public MultiXmlDocumentationProvider(string xmlDocFilesPath)
        {
            _documentationProviders = new List();
    
            foreach (string file in Directory.GetFiles(xmlDocFilesPath, "*.xml"))
            {
                _documentationProviders.Add(new XmlDocumentationProvider(file));
            }
        }
    
        public string GetDocumentation(System.Reflection.MemberInfo member)
        {
            return _documentationProviders
                .Select(x => x.GetDocumentation(member))
                .FirstOrDefault(x => !string.IsNullOrWhiteSpace(x));
        }
    
        //and so on...
    

    The HelpPageConfig registration is the same as in gurra777's answer,

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

提交回复
热议问题