ASP.NET Web API - Part Four

In Part 1 , Part 2 and  Part 3 of this series, we discussed HTTP Verbs and their implementation along with Content Negotiation. Now, let’s discuss custom method names.

Custom Method Names

Now, in the Web API the user maps HTTP verbs Get, Post, Put and Delete methods in the Controller, however, in Controller, you have methods with the same name or they start with the same, like Get() is mapped to Get(), GetEmployees() or GetSomething().

Now, let’s understand with an example.

Go to File >> New Project >> select ASP.NET Web Application and add the name of the project, as shown below.

ASP.NET

Now, select Web API as shown below.

ASP.NET

Now, go to ValueController.

  1. public class ValuesController : ApiController  
  2.   {  
  3.       // GET api/values  
  4.       public IEnumerable<string> Get()  
  5.       {  
  6.           return new string[] { "value1""value2" };  
  7.       }  
  8.   
  9.       // GET api/values/5  
  10.       public string GetEmployee(int id)  
  11.       {  
  12.           return "value";  
  13.       }  
  14.   
  15.       // POST api/values  
  16.       public void Post([FromBody]string value)  
  17.       {  
  18.       }  
  19.   
  20.       // PUT api/values/5  
  21.       public void Put(int id, [FromBody]string value)  
  22.       {  
  23.       }  
  24.   
  25.       // DELETE api/values/5  
  26.       public void Delete(int id)  
  27.       {  
  28.       }  
  29.   }  

Now, if you run the application and try to access http://localhost:XXX /api/values, then it will execute Get(); however, if you will access http://localhost:XXX /api/values/1, then it will execute GetEmployee().

Now, let's see what happens. If the name does not start with Get, so within the example, let's change the name of GetEmployee to DisplayEmployee () as shown below.

  1. public class ValuesController : ApiController  
  2.   {  
  3.       // GET api/values  
  4.       public IEnumerable<string> Get()  
  5.       {  
  6.           return new string[] { "value1""value2" };  
  7.       }  
  8.   
  9.       // GET api/values/5  
  10.       public string DisplayEmployee(int id)  
  11.       {  
  12.           return "value";  
  13.       }  
  14.   
  15.       // POST api/values  
  16.       public void Post([FromBody]string value)  
  17.       {  
  18.       }  
  19.   
  20.       // PUT api/values/5  
  21.       public void Put(int id, [FromBody]string value)  
  22.       {  
  23.       }  
  24.   
  25.       // DELETE api/values/5  
  26.       public void Delete(int id)  
  27.       {  
  28.       }  
  29.   }  

Now, run the application and try to access the http://localhost:XXX /api/values/1. You will get 405 error.

Since the Web API is not able to understand DisplayEmployee(), we can instruct it by decorating HttpGet attribute.

Now, add [HttpGet] attribute to DisplayEmployee() as shown below.

  1.   public class ValuesController : ApiController  
  2.     {  
  3.         // GET api/values  
  4.         public IEnumerable<string> Get()  
  5.         {  
  6.             return new string[] { "value1""value2" };  
  7.         }  
  8.   
  9.         // GET api/values/5  
  10. [HttpGet]   
  11.         public string DisplayEmployee(int id)  
  12.         {  
  13.             return "value";  
  14.         }  
  15.   
  16.         // POST api/values  
  17.         public void Post([FromBody]string value)  
  18.         {  
  19.         }  
  20.   
  21.         // PUT api/values/5  
  22.         public void Put(int id, [FromBody]string value)  
  23.         {  
  24.         }  
  25.   
  26.         // DELETE api/values/5  
  27.         public void Delete(int id)  
  28.         {  
  29.         }  
  30.     }  

Now, run the application and try to access http://localhost:XXX /api/values/1

Similarly, you can use [HttpPost], [HttpPut] and [HttpDelete].

In the next article, we will be discussing  how to call WebAPI using jQuery. 
 
 


Similar Articles