Custom Method Names In ASP.NET Web API

Overview

In this article, we will learn about custom method names in ASP.NET WebAPI and how to create custom method names in an ASP.NET WebAPI Controller.

Introduction

First, let’s understand the default convention in place that ASP.NET WebAPI uses to map the HTTP verbs GET, PUT, POST, and DELETE to the methods in Controller class.

By default, the HTTP verb is mapped to a method in a controller that has the same name as the verb or starts with the verb. For example:

HTTP verb GET mapped to a GET () or GetEmployees () method. So, in our EmployeeController we have GET () method as,



So, by convention, get () is mapped to the HTTP verb GET. Even if we rename it to GetEmployee(), it will still be mapped to HTTP verb GET as long as method name is prefix with the word Get.

The word Get is case insensitive. So, it can be uppercase, lowercase, or mix of both. Let’s change that verb as:



Here, I have changed to GetDetails, built our solution, run the app and let's issue Get request as:



Notice that we are getting list of all employees. So out GetDetails () method within our EmployeesController is mapped to the HTTP verb and it works as expected.

Now if the method is not named as Get or if it does not start with Get, within our employee’s controller let's  change that method name as,



Here, I have changed the method name to LoadData (). If the method name does not start with get, then ASP.NET WebAPI does not know which method within our Controller class to map to the HTTP Verb Get. So, it’s going to fail with an error message as,

The requested resource does not support HTTP method GET. Status Code 405 Method Not Allowed.

Now, let’s issue get request and see what happens,



Notice, we get the error message resource does not support http method GET . So if we inspect this method in fiddler,



Look at the status Code. We have got - "405 Method Not Allowed".

Since, WebAPI does not know to map this method LoadData () to the HTTP verb GET we get that message. We can do this by specifying HTTPGET attribute as,



Let’s run the app and reissue the request as,



Notice, we get the list of all employees as expected. Now let’s request for a specific employee. In the URI we will specify employee ID 1,



Again we had got the desired result -- we got the specific employee details.

Now in this case WebAPI knows to map the GET Verb to this Get method,



Because the method name is Get in the URI we had got Employee ID as 1 and this method got the id parameter. So Get request in this case is mapped to Get method within our Employees Controller.

Now, let’s see what’s going to happen if we change the Get method name as,



Now, let’s build and run our solution



Though we are requesting with an employee ID 1 we are getting a list of employees. So let’s understand what’s going on here -- the request LoadEmployeeID () method is mapped to LoadData () method as,



Here, the LoadData() method is the only method which is declared to HttpGet attribute in this case WebAPI does not know it has to map this Get request to this LoadEmployeeID() method because it’s not declared with Get and it does not start with the word Get. So let’s declare that method with HttpGet attribute as,



When we run our solution and specify that ID in URI we get as,



Notice, we got that particular employee Details whose ID is 1. IF we don’t specify the employee ID we should get list of all employees as,



So, we have used HttpGet attribute to let WebAPI know the two methods we want to map to the HTTP Verb Get, depending on whether we have Employee ID parameter in the URI or not.

Also, we have HttpPut,HttpPost and HttpDelete to map custom named methods in an ASP.NET WebAPI Controller.

Conclusion - So, this was all about Custom method names in ASP.NET Web API. Hope this article was helpful!!