Custom Action Methods In ASP.NET Web API

Introduction

This article explains how to create custom action methods in ASP.NET Web API. In Web API, we use default action methods like Get, Put, Post, and Update.This article helps you to learn how to create custom action methods instead of these default action methods in ASP.NET Web API. Before reading this article, kindly read the previous part of this article from the following link.

Default Action Methods

In Web API, Get, Post, Put, and Delete verbs are used as corresponding action methods-  Get(), Post ([FromBody]string value), Put (int id, [FromBody]string value), Delete (int id) for manipulating data like get, insert, update and delete.

We have added ValueController, default action methods, and manipulating functionalities using static variable. Just look at the below code. 

  1. public class ValuesController : ApiController  
  2.     {  
  3.   
  4.         static List<string> languages = new List<string>() {   
  5.             "C#","ASP.NET","MVC"  
  6.         };          
  7.   
  8.         // GET api/values  
  9.         public IEnumerable<string> Get()  
  10.         {  
  11.             return languages;  
  12.         }  
  13.   
  14.         // GET api/values/5  
  15.         public string Get(int id)  
  16.         {  
  17.             return languages[id];  
  18.         }  
  19.   
  20.         // POST api/values  
  21.         public void Post([FromBody]string value)  
  22.         {  
  23.             languages.Add(value);  
  24.         }  
  25.   
  26.         // PUT api/values/5  
  27.         public void Put(int id, [FromBody]string value)  
  28.         {  
  29.             languages[id] = value;  
  30.         }  
  31.   
  32.         // DELETE api/values/5  
  33.         public void Delete(int id)  
  34.         {  
  35.             languages.RemoveAt(id);  
  36.         }  
  37.   
  38.     }   

Type 1

When we run Web API in ASP.NET, it calls Get () because Get () is mapped into Web API controllers. We can see the following outputs.


Type 2

We will change the action name with prefix Get verb. After changing the action name like GetValues(), run the Web API. Now also, we are getting the same output. In the Web API, Http verb should be used in prefix in every action name.


Type 3

Here, we change the action method name with a fully different one, like “Values()” and build run the web API. Now, our web API returns the error message because action method is not like “Get()” as well as action method does not start with prefix other than “Get” so API could not find action method. We can see error messages as “The requested resource does not support HTTP method 'GET'” XML format looks like the following screenshot.


Now, open the fiddler and run the Web API at the above-mentioned URL. We can see the error code and error message as “405 Method Not Allowed” in the fiddler.


Attribute in Web API

We use an attribute for mapping custom action method with Web API request. The attribute uses denoted type of methods to custom action methods. There are four important attributes we use frequently.

  1. HttpGet
  2. HttpPost
  3. HttpPut
  4. HttpDelete

HttpGet

HttpGet attribute is a sealed class. It is inherited from “Attribute” abstract class and “IActionHttpMethodProvider” interface. Below screenshot shows HttpGet sealed class.


Type 1

HttpGet attribute is mapped to the custom action methods. If we create custom Get method, we need to mention the corresponding attribute on top of the custom method. Now, we are not getting any errors if we create custom action methods.
Type 2

Now, we run the API with particular parameters (here index value). So now, it should return only the corresponding index value string, but here, it returns all values because we did not mention attribute for custom action methods with argument. So, it is calling default without parameter action method.

After having mentioned attribute for “Values (int id)” action methods, now will return exact output to the given request.



For custom Put, Post, and Delete action methods, we use HttpPut, HttpPost, HttpDelete attributes.

Please look at the following code for mapping custom action methods using corresponding attributes. 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Net.Http;  
  6. using System.Web.Http;  
  7.   
  8. namespace WebApplication2.Controllers  
  9. {  
  10.       
  11.     public class ValuesController : ApiController  
  12.     {  
  13.   
  14.         static List<string> languages = new List<string>() {   
  15.             "C#","ASP.NET","MVC"  
  16.         };          
  17.   
  18.         [HttpGet]  
  19.         public IEnumerable<string> Values()  
  20.         {  
  21.             return languages;  
  22.         }  
  23.   
  24.         // GET api/values/5  
  25.         [HttpGet]  
  26.         public string Values(int id)  
  27.         {  
  28.             return languages[id];  
  29.         }  
  30.   
  31.         // POST api/values  
  32.         [HttpPost]  
  33.         public void ValuesPost([FromBody]string value)  
  34.         {  
  35.             languages.Add(value);  
  36.         }  
  37.   
  38.         // PUT api/values/5  
  39.         [HttpPut]  
  40.         public void ValuesPut(int id, [FromBody]string value)  
  41.         {  
  42.             languages[id] = value;  
  43.         }  
  44.   
  45.         // DELETE api/values/5  
  46.         [HttpDelete]  
  47.         public void ValuesDelete(int id)  
  48.         {  
  49.             languages.RemoveAt(id);  
  50.         }  
  51.   
  52.     }  
  53. }   

Conclusion

This article explained about custom action methods in ASP.NET Web API. It will help new learners and freshers.


Similar Articles