Tuesday, April 15, 2014

WebApi: Configuration over Convention using MVC4 template

In this article I’ll share my thoughts on Configuration over Convention in WebApi and how it is being used in 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 give 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, which 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 achieve this.

Step 1: Let’s first create a sample web application and using ASP.NET MVC 4 Web Application and named It with your choice as I gave WebApiDemo shown in depict image below:


Step2: Click ok and choose Web API option from the templates shown in wizard window.


Step3: 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 give a name of EmployeesController for the controller. Leave the option 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.Kinldy add the following code into 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 a name of Employee.


After creating the Employee.cs 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 put a method named as GetEmployeeById, which follows the convention based (starts with Get, PUT, POST, and Delete) approach to call methods of WebApi.

public Employee GetEmployeeById(int id)


return listEmp.First(e => e.ID == id);


Press F5 and run your application it will show the below 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 debugger point. This is the convention based approach (Method Starts with Http Verb Get).





Now move ahead 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:


public Employee FetchEmployeeById(int id)


return listEmp.First(e => e.ID == id);


Kindly put this url (http://localhost:57888/api/Employees/001) in browser and it will be the output as shown below in image.


Conclusion: In this article we looked into Configuration over Convention in WebApi.

Hope it will help you somewhere down the line J

Keep coding and Smile J


Sachin Kalia


Post a Comment