Creating the Help Page in ASP.NET Web API

Introduction

In this article we will define the process of creating the help page in the ASP .NET Web API. For creating the help page first we need to install the ASP .NET and Web Tools 2012.2 update. When we install this update it integrates the help page into the web API.

Step 1

We can install this update from this link: Click me

Step 2

We create the Web API application using the following:

  • Start the Visual Studio 2012.
  • Click on New Project and select the MVC4 application.
  • Now select the Web API application from the template.
help.jpg

help1.jpg

Step 3

Now we see the Areas folder in Solution Explorer. The Areas folder contains the help page folder.

help4.jpg

Step 4

Now we execute the application.

help2.jpg

When we execute the application we will see the API help page Link.

When we click on API help Link then open a API summary page.

help3.jpg

Step 5

There are more links that are connected to the detailed information page. We will see this image for the Response body format.

help5.jpg

Step 6

Adding the API Documentation

For adding the API Documentation go to Areas/HelpPage/App_Start/HelpPageConfig.cs and uncomment the following code in this file.

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

Now we enable the XML Documentation. In the Solution Explorer right-click on the project and select the properties.

help6.jpg

Then open this page.

help7.jpg

In this window we select the Output and check the XML documentation file and in the edit box we type the following line in App_Data/XMLDocument.xml.

help8.jpg

Step 8

Now we open the Valuescontroller API controller and add the some documentation comment For example:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Net.Http;  
  6. using System.Web.Http;  
  7. namespace MvcApplication4.Controllers  
  8. {  
  9.     public class ValuesController : ApiController  
  10.     {  
  11.         /// <summary>  
  12.         ///Fetch some important data from the server.  
  13.         /// </summary>  
  14.         public IEnumerable<string> Get()  
  15.         {  
  16.             return new string[] { "value1""value2" };  
  17.         }  
  18.         /// <summary>  
  19.         /// Take Data by ID.  
  20.         /// </summary>  
  21.         /// <param name="id">The ID of the data.</param>  
  22.         public string Get(int id)  
  23.         {  
  24.             return "value";  
  25.         } 
Step 9

Now we again run the application and we see that the documentation string is shown in the API table.

help9.jpg

We can modify the layout of the API application such as Title, font size, color etcetera. Here we see an example of modifying the formatting of the API.

Here is an Index.cshtml file. For modifying the layout we perform the change in this file. This file exists in the Solution Explorer Areas/HelpPage/Views/Help/Index.cshtml.

help10.jpg

  1. @using System.Web.Http  
  2. @using System.Web.Http.Description  
  3. @using System.Collections.ObjectModel  
  4. @using MvcApplication4.Areas.HelpPage.Models  
  5. @model Collection<ApiDescription>  
  6. @{  
  7.     ViewBag.Title = "This is ASP.NET Web API Help Page";  
  8.     // Group APIs by controller  
  9.     ILookup<string, ApiDescription> apiGroups = Model.ToLookup(api => api.ActionDescriptor.ControllerDescriptor.ControllerName);  
  10. }  
  11. <header>  
  12.     <div class="content-wrapper">  
  13.         <div class="float-left">  
  14.             <h1>@ViewBag.Title</h1>  
  15.         </div>  
  16.     </div>  
  17. </header>  
  18. <div id="body">  
  19.     <section class="featured">  
  20.         <div class="content-wrapper">  
  21.             <h2>  <font color="Blue">Introduction</font></h2>  
  22.             <p>  
  23.                 <font color="Red" size="20pt">  
  24.                 Provide a general description of your APIs here.  
  25.                     </font>  
  26.             </p>  
  27.         </div>  
  28.     </section>  
  29.     <section class="content-wrapper main-content clear-fix">  
  30.         @foreach (var group in apiGroups)  
  31.         {  
  32.             @Html.DisplayFor(m => group, "ApiGroup")  
  33.         }  
  34.     </section>  
  35. </div>  
  36. @section Scripts {  
  37.     <link type="text/css" href="~/Areas/HelpPage/HelpPage.css" rel="stylesheet" />  

Now it can look like this:

help12.jpg


Similar Articles