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.
- public class ValuesController : ApiController
- {
-
- static List<string> languages = new List<string>() {
- "C#","ASP.NET","MVC"
- };
-
-
- public IEnumerable<string> Get()
- {
- return languages;
- }
-
-
- public string Get(int id)
- {
- return languages[id];
- }
-
-
- public void Post([FromBody]string value)
- {
- languages.Add(value);
- }
-
-
- public void Put(int id, [FromBody]string value)
- {
- languages[id] = value;
- }
-
-
- public void Delete(int id)
- {
- languages.RemoveAt(id);
- }
-
- }
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.
- HttpGet
- HttpPost
- HttpPut
- 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.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Web.Http;
-
- namespace WebApplication2.Controllers
- {
-
- public class ValuesController : ApiController
- {
-
- static List<string> languages = new List<string>() {
- "C#","ASP.NET","MVC"
- };
-
- [HttpGet]
- public IEnumerable<string> Values()
- {
- return languages;
- }
-
-
- [HttpGet]
- public string Values(int id)
- {
- return languages[id];
- }
-
-
- [HttpPost]
- public void ValuesPost([FromBody]string value)
- {
- languages.Add(value);
- }
-
-
- [HttpPut]
- public void ValuesPut(int id, [FromBody]string value)
- {
- languages[id] = value;
- }
-
-
- [HttpDelete]
- public void ValuesDelete(int id)
- {
- languages.RemoveAt(id);
- }
-
- }
- }
Conclusion
This article explained about custom action methods in ASP.NET Web API. It will help new learners and freshers.