How Can Xml Documentation for Web API Include Documentation from Beyond the Main Project

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

There is no built-in way to achieve this. However, it requires only a few steps:

  1. Enable XML documentation for your subproject (from project properties / build) like you have for your Web API project. Except this time, route it directly to XmlDocument.xml so that it gets generated in your project's root folder.

  2. Modify your Web API project's postbuild event to copy this XML file into your App_Data folder:

    copy "$(SolutionDir)SubProject\XmlDocument.xml" "$(ProjectDir)\App_Data\Subproject.xml"

    Where Subproject.xml should be renamed to whatever your project's name is plus .xml.

  3. Next open Areas\HelpPage\App_Start\HelpPageConfig and locate the following line:

    config.SetDocumentationProvider(new XmlDocumentationProvider(
    HttpContext.Current.Server.MapPath("~/App_Data/XmlDocument.xml")));

    This is the line you initially uncommented in order to enable XML help documentation in the first place. Replace that line with:

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

    This step ensures that XmlDocumentationProvider is passed the directory that contains your XML files, rather than the specific XML file for your project.

  4. Finally, modify Areas\HelpPage\XmlDocumentationProvider in the following ways:

    a. Replace the _documentNavigator field with:

    private List<XPathNavigator> _documentNavigators = new List<XPathNavigator>();

    b. Replace the constructor with:

    public XmlDocumentationProvider(string appDataPath)
    {
    if (appDataPath == null)
    {
    throw new ArgumentNullException("appDataPath");
    }

    var files = new[] { "XmlDocument.xml", "Subproject.xml" };
    foreach (var file in files)
    {
    XPathDocument xpath = new XPathDocument(Path.Combine(appDataPath, file));
    _documentNavigators.Add(xpath.CreateNavigator());
    }
    }

    c. Add the following method below the constructor:

    private XPathNavigator SelectSingleNode(string selectExpression)
    {
    foreach (var navigator in _documentNavigators)
    {
    var propertyNode = navigator.SelectSingleNode(selectExpression);
    if (propertyNode != null)
    return propertyNode;
    }
    return null;
    }

    d. And last, fix all compiler errors (there should be three) resulting in references to _documentNavigator.SelectSingleNode and remove the _documentNavigator. portion so that it now calls the new SelectSingleNode method we defined above.

This Last step is what modifies the document provider to support looking within multiple XML documents for the help text rather than just the primary project's.

Now when you examine your Help documentation, it will include XML documentation from types in your related project.

Web API help pages - use XML comments from other projects and base classes

I solved this as follows:

  1. To handle XML files from multiple projects I've incorporated the code from this answer. This code essentially does what @lcryder suggested but automatically.
  2. To apply XML documentation from an interface to its implemented class I am using GhostDoc's Document This option to copy the XML documentation.

However I'm not happy with #2 because it requires manual intervention and I have to remember to recopy the XML documentation whenever it changes, so I'm still looking for a better answer.

Web Api Help Page XML comments from more than 1 files

You can modify the installed XmlDocumentationProvider at Areas\HelpPage to do something like following:

Merge multiple Xml document files into a single one:

Example code(is missing some error checks and validation):

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

Access is denied on build server when deploying Web API project with XML documentation

If the XML file is checked-in to TFS then when TFS gets the files to the workspace on the build server, it will have "Read-Only attribute" associated with the file. Now when the build generates the new XML file, it wont be able to overwrite the old XML file since it has the read-only attribute.

Solution is to:
a) use your build scripts to modify the file attribute and make it read-write
b) remove the xml file checked-in to TFS so that, build will be able to generate the XML easily.

Update: if you are using solution b, if the file is part of you project file make sure that you remove the file from the csproj file as well.

ASP.NET Web API Help page under separate project

You can re-write XmlDocumentationProvider constructor to something like that:

public XmlDocumentationProvider(string appDataPath)
{
if (appDataPath == null)
{
throw new ArgumentNullException(nameof(appDataPath));
}

var files = new[] { "MyWebApiProject.xml" /*, ... any other projects */ };
foreach (var file in files)
{
var xpath = new XPathDocument(Path.Combine(appDataPath, file));
_documentNavigators.Add(xpath.CreateNavigator());
}
}

It will include all the xml documentation files that you list in the array. You should also make sure your target Web API project (and all the model projects if needed) generates documentation on build and copies it to the right location.
Sample Image

How to make WebApi HelpPage recognize model documentations located outside the WebApi project itself

For each external library that contains documentation, you can setup a post build event that copies the locally generated xml file to a central "documentation" folder. This would obviously require that each project that is generating xml docs to uniquely name the file.

Your web api project can then run a post build event to copy whichever xml files it needs from the central shared library into its own project folder. You then need to tweak the xml documentation provider to look for multiple xml files as shown in this post

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



Related Topics



Leave a reply



Submit