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
- public class Student
- {
- [Required]
- public int id { get; set; }
- public string name { get; set; }
- public string Address { get; set; }
- }
TestController.cs
- public class TestController : ApiController
- {
-
-
-
-
-
-
- [HttpGet]
- public List<Student> GetList()
- {
-
- List<Student> lts = new List<Student>();
- lts.Add(new Student { id = 1, name = "atul", Address = "roorkee" });
- lts.Add(new Student { id = 2, name = "chander", Address = "noida" });
-
- return lts;
- }
-
-
-
-
-
- [HttpGet]
- public string getbyid(int id)
- {
- List<Student> lts = new List<Student>();
-
- string result = lts.Find(x => x.id == id).ToString();
- return result;
-
- }
-
-
-
-
-
- [HttpPost]
-
- public HttpResponseMessage Post()
- {
- var returnValue = new Student { id = 1, name = "atul", Address = "roorkee" };
- var response = Request.CreateResponse<Student>(HttpStatusCode.Created, returnValue);
- response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = returnValue.id }));
- return response;
- }
-
-
-
-
-
- [AcceptVerbs("Custom")]
- public HttpResponseMessage Custom()
- {
- return new HttpResponseMessage
- {
- Content = new StringContent("action completed.")
- };
- }
-
-
-
-
- [AcceptVerbs("AtulTest")]
- public HttpResponseMessage CustomTest()
- {
- return new HttpResponseMessage
- {
- Content = new StringContent("action under Processing.")
- };
- }
-
-
- }
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.
- 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: