Creating Help Page For ASP.NET Web API

Intoduction
 
Help page plays a very important role in a developer’s life. It is useful to create help page because every developer should know how to call Web API and know what the behavior of the method is and know some hints about the method or call.
 
Web API have by Default a help page. If we want to extend the help page document from user understandability, let’s see the procedure and create a Help page step by step
 
Create MVC Application
 
Step 1
 
Go to File, New, then Project.
 
Step 2
 
Choose "ASP.NET MVC 4 Web Application" from the list, then provide the application name as " HelpPageNuget " and set the path in the location input where you want to create the application.
 
Step 3
 
Now choose the Project Template "Web API".
 
Step 4
 
Add API empty controller and Class with name “Test”
 
Step 5
 
From the Tools menu, select Library Package Manager, and then select Package Manager Console. In the Package Manager Console window, type one of the following commands
 
“Install-Package Microsoft.AspNet.WebApi 2.2 .HelpPage”
 
If you already have Area Folder delete the Area folder and install above nugget package because this nugget extend some new methods inside your Area folder.
 
 
Now Run your Project , In your Project the controller folder contains the ValuesController class . Let see the help page of VlauesController. in the depicted figure.
 
  
Step 6
 
Right click on project and select property. 
 
 
 
Now new tab open in next to you after that click build in you left side template. And checked the XML Documentation file. And provide the path same as depicted figure. 
 
 
Add new Testcontroller in our project and add some different type of return methods
 
  1. public class Student  
  2.     {  
  3.         [Required]  
  4.         public int id { getset; }  
  5.         public string name { getset; }  
  6.         public string Address { getset; }  
  7.     }  
 TestController.cs
  1. public class TestController : ApiController  
  2.    {  
  3.        /// <summary>  
  4.        /// retrun all record  
  5.        /// </summary>  
  6.        /// <param name="id"></param>  
  7.        /// <returns></returns>  
  8.   
  9.        [HttpGet]  
  10.        public List<Student> GetList()  
  11.        {  
  12.   
  13.            List<Student> lts = new List<Student>();  
  14.            lts.Add(new Student { id = 1, name = "atul", Address = "roorkee" });  
  15.            lts.Add(new Student { id = 2, name = "chander", Address = "noida" });  
  16.   
  17.            return lts;  
  18.        }  
  19.        /// <summary>  
  20.        /// retrun record on the bases of id  
  21.        /// </summary>  
  22.        /// <param name="id"></param>  
  23.        /// <returns></returns>  
  24.        [HttpGet]  
  25.        public string getbyid(int id)  
  26.        {  
  27.            List<Student> lts = new List<Student>();  
  28.   
  29.            string result = lts.Find(x => x.id == id).ToString();  
  30.            return result;  
  31.   
  32.        }  
  33.        /// <summary>  
  34.        ///  retrun response   
  35.        /// </summary>  
  36.        /// <param name="id"></param>  
  37.        /// <returns></returns>  
  38.        [HttpPost]  
  39.        // [ApiExplorerSettings(IgnoreApi = true)]  
  40.        public HttpResponseMessage Post()  
  41.        {  
  42.            var returnValue = new Student { id = 1, name = "atul", Address = "roorkee" };  
  43.            var response = Request.CreateResponse<Student>(HttpStatusCode.Created, returnValue);  
  44.            response.Headers.Location = new Uri(Url.Link("DefaultApi"new { id = returnValue.id }));  
  45.            return response;  
  46.        }  

  47.   
  48.        /// <summary>  
  49.        /// retrun response  
  50.        /// </summary>  
  51.        /// <returns></returns>  
  52.        [AcceptVerbs("Custom")]  
  53.        public HttpResponseMessage Custom()  
  54.        {  
  55.            return new HttpResponseMessage  
  56.            {  
  57.                Content = new StringContent("action completed.")  
  58.            };  
  59.        }  
  60.        /// <summary>  
  61.        /// retrun response  
  62.        /// </summary>  
  63.        /// <returns></returns>  
  64.        [AcceptVerbs("AtulTest")]  
  65.        public HttpResponseMessage CustomTest()  
  66.        {  
  67.            return new HttpResponseMessage  
  68.            {  
  69.                Content = new StringContent("action under Processing.")  
  70.            };  
  71.        }  
  72.   
  73.   
  74.    }  
 
All the summery of methods is save in Xml file. After that we need to read the xml file from the directory for that add a line of code inside the Areafolder/App_start/HelpPageConfig.cs file.
 
  1. config.SetDocumentationProvider(new XmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/App_Data/HelpPageNuget.xml")));  
Run your Project with the help of F5 key hit below URL
 
http://yourlocal host/help see the output in below figure.
 
 
Summary
 
In this article, we learned how to create HelpPage in Web API
 
Read more articles on ASP.NET


Similar Articles