Web Api Help Page XML comments from more than 1 files

前端 未结 4 1048
孤街浪徒
孤街浪徒 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:37

    Kirans solution works very well. I ended up using his approach but by creating a copy of XmlDocumentationProvider, called MultiXmlDocumentationProvider, with an altered constructor:

    public MultiXmlDocumentationProvider(string xmlDocFilesPath)
    {
           XDocument finalDoc = null;
            foreach (string file in Directory.GetFiles(xmlDocFilesPath, "*.xml"))
            {
                using (var fileStream = File.OpenRead(file))
                {
                    if (finalDoc == null)
                    {
                        finalDoc = XDocument.Load(fileStream);
                    }
                    else
                    {
                        XDocument xdocAdditional = XDocument.Load(fileStream);
    
                        finalDoc.Root.XPathSelectElement("/doc/members")
                            .Add(xdocAdditional.Root.XPathSelectElement("/doc/members").Elements());
                    }
                }
            }
    
            // Supply the navigator that rest of the XmlDocumentationProvider code looks for
            _documentNavigator = finalDoc.CreateNavigator();
    }
    

    I register the new provider from HelpPageConfig.cs:

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

    Creating a new class and leaving the original one unchanged may be more convenient when upgrading etc...

提交回复
热议问题