In this article, we will see HTTP verb attributes on API method. This is part four of the article series. Before reading this article, I would recommend reading the following previous part.
- All about API: Introduction to Web API - Part One
- All about API: Using HTTP Methods - Part Two
- All About API: HTTP Response Message - Part Three
Normally method invoked is picked based upon the verb of the incoming HTTP Request like Get, Post, Put and Delete. But consider a scenario where you don’t want to name your methods with the convention, you can add attributes to your methods. You can name method whatever you would like to and set specific attributes like,
- HTTPGet
- HTTPPost
- HTTPPut
- HTTPDelete
Routing engine still picks the controller and method.
Let’s see the same step by step:
- Open Visual Studio Editor.
- Select ‘File’ menu, expand ‘New’ and click on ‘Project…’
- Expand ‘Visual C#’ and select ‘Web’ from the left panel.
- Select ‘ASP.NET Web Application’.
- Provide appropriate name of the application and select the location.
- Click on ‘OK’ button.
Select ‘Empty’ as a template and check ‘Web API’ check box.
Press ‘OK’ button and it will create an empty Web API project.
- Right click on ‘Controllers’, expand ‘Add’ and click on ‘Controller…’
- Select ‘Web API 2 Controller with read/write actions’ and press ‘Add’ button, which will add all HTTP methods,
- Provide appropriate name and press ‘Add’ button,
Here we add HttpGet for GET method, HTTPPost for POST method, HTTPPut for PUT method and HTTPDelete for DELETE method.
We set meaningful names for the method like ‘GetAllServices’, ‘AddService’ etc.
namespace VerbsToAttributes.Controllers
{
public class ServiceController: ApiController
{
static List < string > serviceData = LoadService();
public static List < string > LoadService()
{
return new List < string > () {
"Mobile Recharge",
"Bill Payment"
};
}
[HttpGet]
public HttpResponseMessage GetAllServices()
{
return Request.CreateResponse < IEnumerable < string >> (HttpStatusCode.OK, serviceData);
}
[HttpGet]
public HttpResponseMessage GetServiceById(int id)
{
if (serviceData.Count > id) return Request.CreateResponse < string > (HttpStatusCode.OK, serviceData[id]);
else return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Item Not Found");
}
[HttpPost]
public HttpResponseMessage AddService([FromBody] string value)
{
serviceData.Add(value);
return Request.CreateResponse(HttpStatusCode.Created, "Item Added Successfully");
}
[HttpPut]
public HttpResponseMessage UpdateService(int id, [FromBody] string value)
{
serviceData[id] = value;
return Request.CreateResponse(HttpStatusCode.OK, "Item Updated Successfully");
}
[HttpDelete]
public HttpResponseMessage DeleteService(int id)
{
serviceData.RemoveAt(id);
return Request.CreateResponse(HttpStatusCode.OK, "Item Deleted Successfully");
}
}
}
Run your application and follow below steps:
GET
- Select ‘GET’ as Method.
- Copy URL and press ‘SEND’ button.
- Observe the response, we can see two string elements are added in the list.
GET By ID
- In order to get the individual string element from the list, pass index in the URL.
- Check the response, you will have individual element associated with the index.
POST
- Select ‘POST’ as method.
- Add Content-Type header with the value ‘application/json’.
- Add the value which you want to add in the list in Body and press ‘SEND’ button.
PUT
- Select ‘PUT’ as method and add index for which you want to modify in the URL.
- Add Content-Type header with value ‘application/json’.
- Add updated value in the body and press ‘SEND’ button.
DELETE
- Select ‘DELETE’ as method and add index for which you want to delete from the list in URL.
- Press ‘SEND’ button.