Asp.net Web API Help Page Customization

After you have set up the Web API help page as shown here, you could do the following customizations:

  • List APIs by controller name
  • List Model properties by name
  • Multi-project support (Model classes in a separate project)

List APIs by controller name

Edit Areas\HelpPage\Views\Index.cshtml and replace the below line

ILookup apiGroups = Model.ToLookup(api=> api.ActionDescriptor.ControllerDescriptor);
with
ILookup apiGroups = Model.OrderBy(d=>d.ActionDescriptor.ControllerDescriptor.ControllerName).ToLookup(api=> api.ActionDescriptor.ControllerDescriptor); 

That way, it would be easy for anyone to refer to the help page documentation, and a person can be sure that all the APIs will be in ascending order.

List Model properties by name

The property’s default order is when you added properties to the Model class. To list down properties in ascending order then, edit Areas\HelpPage\Views\DsplayTemplates\parameters.cshtml and replace the below line:

@foreach (ParameterDescription parameter in Model)

with

@foreach (ParameterDescription parameter in Model.OrderBy(c=>c.Name))

Multi-project support

It is standard practice to have layered architecture; hence, model classes could be in a different project. Please refer to this Stackoverflow post for the same. I’ve modified it a bit to generate the help documentation; please follow the steps mentioned below:

  1. Enable XML documentation for your subproject (from project properties/build) as you have for your Web API project. Except for this time, route it directly to projectname.XmlDocument.xml So that it gets generated in your project’s root folder. Replace projectname with the name of your project.
  2. Modify your Web API project’s post-build event to copy this XML file into your App_Data folder:
copy "$(SolutionDir)SubProject\XmlDocument.xml" "$(ProjectDir)\App_Data\Subproject.xml"

Note: Paths are wrapped in double quotes to handle space in-between paths.

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

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

And replace it with the following:

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

Update Areas\HelpPage\XmlDocumentationProvider as shown below:

Replace the _documentNavigator field with:

private List _documentNavigators = new List();

Replace the constructor with:

public XmlDocumentationProvider(string appDataPath)
        {
            if (appDataPath == null)
            {
                throw new ArgumentNullException("appDataPath");
            }
            foreach (var file in Directory.GetFiles(appDataPath, "*.xml"))
            {
                XPathDocument xpath = new XPathDocument(Path.Combine(appDataPath, file));
                _documentNavigators.Add(xpath.CreateNavigator());
            }
        }

Note: Above constructor is different than the original Stackoverflow post. Here we’re not defining a list of files but will consider all the files having .xml extension.

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

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.

For advanced customization, please refer to this.

I hope it helps!


Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: