Web Api Help Page XML comments from more than 1 files

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

    Rather than create a separate class along the lines of XmlMultiDocumentationProvider, I just added a constructor to the existing XmlDocumentationProvider. Instead of taking a folder name, this takes a list of strings so you can still specify exactly which files you want to include (if there are other xml files in the directory that the Documentation XML are in, it might get hairy). Here's my new constructor:

    public XmlDocumentationProvider(IEnumerable documentPaths)
    {
        if (documentPaths.IsNullOrEmpty())
        {
            throw new ArgumentNullException(nameof(documentPaths));
        }
        XDocument fullDocument = null;
        foreach (var documentPath in documentPaths)
        {
            if (documentPath == null)
            {
                throw new ArgumentNullException(nameof(documentPath));
            }
    
            if (fullDocument == null)
            {
                using (var stream = File.OpenRead(documentPath))
                {
                    fullDocument = XDocument.Load(stream);
                }
            }
            else
            {
                using (var stream = File.OpenRead(documentPath))
                {
                    var additionalDocument = XDocument.Load(stream);
                    fullDocument?.Root?.XPathSelectElement("/doc/members").Add(additionalDocument?.Root?.XPathSelectElement("/doc/members").Elements());
                }
            }
        }
    
        _documentNavigator = fullDocument?.CreateNavigator();
    }
    

    The HelpPageConfig.cs looks like this. (Yes, it can be fewer lines, but I don't have a line limit so I like splitting it up.)

    var xmlPaths = new[]
    {
        HttpContext.Current.Server.MapPath("~/bin/Path.To.FirstNamespace.XML"),
        HttpContext.Current.Server.MapPath("~/bin/Path.To.OtherNamespace.XML")
    };
    var documentationProvider = new XmlDocumentationProvider(xmlPaths);
    config.SetDocumentationProvider(documentationProvider);
    

提交回复
热议问题