Skip to main content

How To Create Help Pages(Documents) For Asp.Net Web API

In this tutorial i am going to explain about how to create help pages to asp.net web api using Microsoft.AspNet.WebApi.HelpPage  package. Here i will explain how to add the help page for existing Web API as well as the new Web API Project that has to be created.


In my previous article i have explained about Store Custom Objects In Configuration File , Create Simple Mathmatical Captcha In ASP.Net , Visual Studio Keyboard Shortcuts , How To get data from WEB API , How To Create Your First WEB API Project, Check Container Exists In Azure Blob and many articles in C#.Net,VB.Net,Grid View,Javascript,jQuery,SQL Server and many other topics.

Whenever we create Web API it is very useful and must to create the help pages so that other developers who consumes /uses the web api for development will know for which purpose a particular web api is used. To make this task easier, ASP.NET Web API provides a library for auto-generating help pages at run time. Before proceeding to create WEB API helppages you suppose to install ASP.NET and Web Tools 2012.2 Update this update will integrate helppages into web api template.

Now please look into my previous article How To Create Your First WEB API Project to create the web api project. Now i will explain about how to add help pages to an existing web api project.

After creating your web api project go to Tools ==> NuGet Package Manager ==> Package Manager Console as shown in the below figure.

Step 1 of How To Create Help Pages For Asp.Net Web API

Now type the below command.
For C#.Net: Install-Package Microsoft.AspNet.WebApi.HelpPage
For VB.Net:Install-Package Microsoft.AspNet.WebApi.HelpPage.VB

This command will install all the necessary assemblies and MVC views to render helppage. After installing you can see a folder named Areas in your project. Inside the areas folder you can see controller for help and views like shown in below figure.
Step 2 of How To Create Help Pages For Asp.Net Web API
Now we have to register Areas. Open the Global.asax file and in the Application_Start method add the AreaRegistration.RegisterAllAreas() code. AreaRegistration class is available under System.Web.Mvc namespace. So you may need to include this namespace if it is not already there. The entire code is given below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Routing;
using System.Web.Mvc;

namespace ypapi
{
    public class WebApiApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            GlobalConfiguration.Configure(WebApiConfig.Register);
           
            // Add this code, if not present.
            AreaRegistration.RegisterAllAreas();
        }
    }
}

Now if you run the application the relative URI for the helppage is /Help. Below is the sample output.
Output 1 of How To Create Help Pages For Asp.Net Web API
By default the help page will have placeholder text for documentation.You can use XML Documentation comments for to create the documentation. For this open the file Areas/HelpPage/App_Start/HelpPageConfig.cs and uncomment the following line.

//// Uncomment the following to use the documentation from XML documentation file.
config.SetDocumentationProvider(new XmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/App_Data/XmlDocument.xml")));

And then right click the project in solution explorer then select Properties then Build page like below.Under Output, check XML documentation file and In the edit box, type "App_Data/XmlDocument.xml".

Step 4 of How To Create Help Pages For Asp.Net Web API

Now open your controller (Any Controller not the help controller) and add the xml comment to each action and also you can add xml comment for the controller too like below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebApiDemo.Models;
// Add this code, if not present.
using System.Web.Http.Description;

namespace WebApiDemo.Controllers
{
    /// <summary>
    /// It has the APIs related to employees
    /// </summary>
    public class EmployeeController : ApiController
    {
        Employee[] employees = new Employee[]
        {
            new Employee { EmployeeId = 1, EmployeeName = "Kannadasan", Department = "Development", Designation = "Development Lead" },
            new Employee { EmployeeId = 2, EmployeeName = "Kaviyarasan", Department = "Quality Control", Designation = "Quality Lead" },
            new Employee { EmployeeId = 3, EmployeeName = "Xavier", Department = "Development", Designation = "Tech Lead" },
            new Employee { EmployeeId = 4, EmployeeName = "Ramkumar", Department = "Design", Designation = "Design Lead" }
        };

        /// <summary>
        /// Returns list of all the employees
        /// </summary>
        /// <returns></returns>
       
        public IEnumerable<Employee> GetAllEmployees()
        {
            return employees;
        }

        /// <summary>
        /// Returns the specific employee record
        /// based on the employee id
        /// </summary>
        /// <param name="id">EmployeeId</param>
        /// <returns></returns>
        public IHttpActionResult GetEmployee(int id)
        {
            var employee = employees.FirstOrDefault((p) => p.EmployeeId == id);
            if (employee == null)
            {
                return NotFound();
            }
            return Ok(employee);
        }
    }
}


Now if you run the application you will see the api with description grouped by each controller. If you click on each API it will provide the detailed documentation of API properties and sample json and xml outputs like below.
Output 2 of How To Create Help Pages For Asp.Net Web API

You can exclude a particular API or even a Controller if you don't want to add it in the Web API help page by using ApiExplorerSettings(IgnoreApi=true) attribute (ApiExplorerSettings is available in the System.Web.Http.Description namespace. You suppose to include it in the page). The sample code is given below

[ApiExplorerSettings(IgnoreApi = true)]
public IEnumerable<Employee> GetAllEmployees()
{
    return employees;
}

After excluding the API the output will be as shown in figure

Output 3 of How To Create Help Pages For Asp.Net Web API

SourceCode:

Sourcecode of How To Create Help Pages For Asp.Net Web API

If you like this article then share with your friends and comment your valuable feedback.. Happy coding..

You May Also Like...

Comments

Popular posts from this blog

Code To Convert rupees(numbers) into words using C#.Net

Introduction: In my previous article I have explained about how to validate emailid using javascript . In this article I am going to explain about code used to convert rupees(numbers) into words using C#.Net . Explanation: For explanation purpose I have a page. It has a textbox to input the numbers. And when you click on the convert to words button then it will convert the input numbers into words and shows it in the below label. Below is the C# code used to do this functionality. public static string NumbersToWords( int inputNumber) {     int inputNo = inputNumber;     if (inputNo == 0)         return "Zero" ;     int [] numbers = new int [4];     int first = 0;     int u, h, t;     System.Text. StringBuilder sb = new System.Text. StringBuilder ();     if (inputNo < 0)     { ...

C# Extension Methods Example

In this article i am going to explain about Extension Methods  with example. Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type. For client code written in C# and Visual Basic, there is no apparent difference between calling an extension method and the methods that are actually defined in a type. If you do implement extension methods for a given type, remember the following points: An extension method will never be called if it has the same signature as a method defined in the type. Extension methods are brought into scope at the namespace level. For example, if you have multiple static classes that contain extension methods in a single namespace named Extensions, they will all be brought into scope by the using Extensions; directive. ...

VBScript equivalent string functions in C#.Net

C# Method VBScript Description IndexOf() InStr Returns the position of the first occurrence of one string within another. The search begins at the first character of the string - InStrRev Returns the position of the first occurrence of one string within another. The search begins at the last character of the string ToLower() LCase Converts a specified string to lowercase SubString() Left Returns a specified number of characters from the left side of a string Length() Len Returns the number of characters in a string TrimStart() LTrim Removes spaces on the left side of a string TrimEnd() RTrim Removes spaces on the right side of a string Trim() Trim Removes spaces on both the left and the right side of a string SubString() M...