In this article, I'll share my thoughts on Configuration over Convention in the WebApi and how it is being used in the WebApi.
You will override the default behavior of the ASP.NET Web API framework in selecting the action method of the controller based on the HTTP method. The default convention is to provide the action method the same name as the HTTP method or to name the method so that it starts with the HTTP method. For example, I used Get in the previous exercise to handle HTTP GET. So the action method can be GetEmployee, or GetEmployeeById.
Similarly, the action methods of Post, Put, and Delete will respectively handle HTTP POST, PUT, and DELETE. Of course, an action method with a weird name such as PutTheShakeinCurd can still be matched by the ASP.NET Web API framework to handle HTTP PUT because the method name begins with Put, that corresponds to HTTP PUT. To override this convention, you can apply the AcceptVerbs attribute or use the equivalent shorthand notation of HttpGet, HttpPost, HttpPut, HttpDelete, and so on.
There are certain steps to do this.
Step 1: Let's first create a sample ASP.NET MVC 4 Web Application and name it as you chosse; I used WebApiDemo as shown in the image below:
Step 2: Click OK and choose the Web API option from the templates shown in the wizard window.
Step 3: You'll find the application structure as shown below at first sight.
Step 4: Right-click the Controllers folder in the Solution Explorer of Visual Studio. Select "Add" ➤"Controller" and provide a name of EmployeesController for the controller. Leave the option for Empty API Controller selected in the Template dropdown and click "Add", as shown in figure below. Notice that the generated controller class inherits from ApiController, a class that is part of the ASP.NET Web API framework. Kindly add the following code into the EmployeesController class.
- public static IList<Employee> listEmp = new List<Employee>()
- {
- new Employee()
- {
- ID =001, FirstName="Sachin", LastName="Kalia"
- },
- new Employee()
- {
- ID =002, FirstName="Dhnanjay" ,LastName="Kumar"
- },
- new Employee()
- {
- ID =003, FirstName="Ravish", LastName="Sindhwani"
- },
- new Employee()
- {
- ID =004, FirstName="Amit" ,LastName="Chaudhary"
- },
- };
Step 5: Right-click the Models folder in the Solution Explorer of Visual Studio. Select "Add" -> "Class" to add a new class with the name "Employee".
After creating the Employee class, kindly add the following code into this class.
- public class Employee
- {
- public string ID { get; set; }
- public string FirstName { get; set; }
- public string LastName { get; set; }
- }
Now I've created a method named "s" GetEmployeeById that follows the convention (starts with Get, PUT, POST and Delete) approach to call methods of the WebApi.
- public Employee GetEmployeeById(int id)
- {
- return listEmp.First(e => e.ID == id);
- }
Press F5 and run your application; it will show the following image:
Great WebApi is up and running.
Kindly hit this URL http://localhost:57888/api/Employees/002 and it will reach into your code segment. Where you've set a debugging breakpoint. This is the convention based approach (Method Starts with Http Verb Get).
Now proceed and make some amendments and adopt the Configuration over Convention.
Add the FetchEmployeeById method to the EmployeesController class, as shown below. An action method that does not follow the naming convention mapped to the required HTTP method through the usage of AcceptVerbs and HttpGet attributes.
Kindly hit this URL http://localhost:57888/api/Employees/002 and it will jump into your code segment. This is the configuration over convention based approach (Method decorated with Http Verb).
You can also decorate your method with this way:
- [HttpGet]
- public Employee FetchEmployeeById(int id)
- {
- return listEmp.First(e => e.ID == id);
- }
Kindly put this URL (http://localhost:57888/api/Employees/001) in the browser and it will be the output as shown below in the image.
Conclusion
In this article, we looked into Configuration over Convention in the WebApi.