Configuration Over Convention in WebApi Using MVC4 Template

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:

MVC 4 Web Application

Step 2: Click OK and choose the Web API option from the templates shown in the wizard window.

Web API option

Step 3: You'll find the application structure as shown below at first sight.

structure

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.

  1. public static IList<Employee> listEmp = new List<Employee>()  
  2. {  
  3.     new Employee()  
  4.     {  
  5.         ID =001, FirstName="Sachin", LastName="Kalia"  
  6.     },  
  7.     new Employee()  
  8.     {  
  9.         ID =002, FirstName="Dhnanjay" ,LastName="Kumar"  
  10.     },  
  11.     new Employee()  
  12.     {  
  13.         ID =003, FirstName="Ravish", LastName="Sindhwani"  
  14.     },  
  15.     new Employee()  
  16.     {  
  17.         ID =004, FirstName="Amit" ,LastName="Chaudhary"  
  18.     },  
  19. }; 
Controllers folder 

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".

add a new class

After creating the Employee class, kindly add the following code into this class.

  1. public class Employee  
  2. {  
  3.     public string ID { getset; }  
  4.     public string FirstName { getset; }  
  5.     public string LastName { getset; }  
  6. } 

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.

  1. public Employee GetEmployeeById(int id)  
  2. {  
  3.     return listEmp.First(e => e.ID == id);  
  4. } 

Press F5 and run your application; it will show the following image:

run your application

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).

debugger point

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).
 
output

You can also decorate your method with this way:
  1. [HttpGet]  
  2. public Employee FetchEmployeeById(int id)  
  3. {  
  4.     return listEmp.First(e => e.ID == id);  
  5. } 

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.

image
Conclusion

In this article, we looked into Configuration over Convention in the WebApi.


Similar Articles