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"
- },
- };






Join the conversation! Your thoughts help the community grow.