Strona pomocy Web Api XML komentarze z więcej niż 1 plików

Mam różne wtyczki w moim projekcie Web api z własnymi dokumentami XML i mam jedną scentralizowaną stronę pomocy, ale problem polega na tym, że domyślna Strona pomocy Web Api obsługuje tylko jeden plik dokumentacji

new XmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/App_Data/Documentation.xml"))

Jak moĹźna wczytywaä ‡ konfiguracjÄ ™ z róşnych plikĂłw? Chcę zrobić sth TAK:

new XmlDocumentationProvider("PluginsFolder/*.xml")
Author: Kara, 2014-03-04

4 answers

Możesz zmodyfikować zainstalowany XmlDocumentationProvider w Areas\HelpPage, aby zrobić coś takiego:

Scalanie wielu plików dokumentów Xml w jeden:

Przykładowy kod (brakuje sprawdzania błędów i walidacji):

using System.Xml.Linq;
using System.Xml.XPath;

 XDocument finalDoc = null;
 foreach (string file in Directory.GetFiles(@"PluginsFolder", "*.xml"))
 {
    if(finalDoc == null)
    {
        finalDoc = XDocument.Load(File.OpenRead(file));
    }
    else
    {
        XDocument xdocAdditional = XDocument.Load(File.OpenRead(file));

        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();
 29
Author: Kiran Challa,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-03-21 15:01:28

Rozwiązanie Kiransa działa bardzo dobrze. Ostatecznie wykorzystałem jego podejście, ale tworząc kopię XmlDocumentationProvider , nazwaną MultiXmlDocumentationProvider , ze zmienionym konstruktorem:

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();
}

Rejestruję nowego dostawcę Z HelpPageConfig.cs:

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

Utworzenie nowej klasy i pozostawienie oryginalnej bez zmian może być wygodniejsze podczas aktualizacji itp...

 19
Author: gurra777,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-05-21 08:32:49

Zamiast tworzyć osobną klasę wzdłuż linii xmlmultidocumentationprovider, dodałem konstruktor do istniejącego xmldocumentationprovider. Zamiast nazwy folderu, pobiera listę ciągów znaków, dzięki czemu możesz nadal dokładnie określić, które Pliki chcesz dołączyć (jeśli są inne pliki xml w katalogu, w którym znajduje się Documentation XML, to Może stać się owłosiony). Oto mój nowy konstruktor:

public XmlDocumentationProvider(IEnumerable<string> 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();
}

HelpPageConfig.cs wygląda tak. (Tak, może być mniej linii, ale nie mam limitu linii, więc lubię je dzielić.)

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);
 5
Author: jack,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-11-17 19:44:52

Zgadzam się z gurra777, że tworzenie nowej klasy jest bezpieczniejszą ścieżką aktualizacji. Zacząłem od tego rozwiązania, ale wiąże się to z sporą ilością kopii / makaronu, które mogą łatwo się nieaktualne po kilku aktualizacjach pakietu.

Zamiast tego przechowuję kolekcję dzieci. Dla każdej z metod implementacji wzywam dzieci do pobrania pierwszego niepustego wyniku.
public class MultiXmlDocumentationProvider : IDocumentationProvider, IModelDocumentationProvider
{
    private IList<XmlDocumentationProvider> _documentationProviders;

    public MultiXmlDocumentationProvider(string xmlDocFilesPath)
    {
        _documentationProviders = new List<XmlDocumentationProvider>();

        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...

Rejestracja HelpPageConfig jest taka sama jak w odpowiedzi gurra777,

config.SetDocumentationProvider(new MultiXmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/App_Data/")));
 4
Author: pleepleus88,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-11-21 22:20:09