All About API: HTTP Verb Attributes - Part Four

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.

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…’

    new

  • 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.

    ok

  • Select ‘Empty’ as a template and check ‘Web API’ check box.

  • Press ‘OK’ button and it will create an empty Web API project.

    empty

  • Right click on ‘Controllers’, expand ‘Add’ and click on ‘Controller…’

    Controller

  • Select ‘Web API 2 Controller with read/write actions’ and press ‘Add’ button, which will add all HTTP methods,

    methods

  • Provide appropriate name and press ‘Add’ button,

    add

  • 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.
    1. namespace VerbsToAttributes.Controllers  
    2. {  
    3.     public class ServiceController: ApiController  
    4.     {  
    5.         static List < string > serviceData = LoadService();  
    6.         public static List < string > LoadService()  
    7.           
    8.         {  
    9.                 return new List < string > () {  
    10.                     "Mobile Recharge",  
    11.                     "Bill Payment"  
    12.                 };  
    13.             }  
    14.             [HttpGet]  
    15.         public HttpResponseMessage GetAllServices()  
    16.         {  
    17.                 return Request.CreateResponse < IEnumerable < string >> (HttpStatusCode.OK, serviceData);  
    18.             }  
    19.             [HttpGet]  
    20.         public HttpResponseMessage GetServiceById(int id)   
    21.         {  
    22.                 if (serviceData.Count > id) return Request.CreateResponse < string > (HttpStatusCode.OK, serviceData[id]);  
    23.                 else return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Item Not Found");  
    24.             }  
    25.             [HttpPost]  
    26.         public HttpResponseMessage AddService([FromBody] string value)   
    27.         {  
    28.                 serviceData.Add(value);  
    29.                 return Request.CreateResponse(HttpStatusCode.Created, "Item Added Successfully");  
    30.             }  
    31.             [HttpPut]  
    32.         public HttpResponseMessage UpdateService(int id, [FromBody] string value)   
    33.         {  
    34.                 serviceData[id] = value;  
    35.                 return Request.CreateResponse(HttpStatusCode.OK, "Item Updated Successfully");  
    36.             }  
    37.             [HttpDelete]  
    38.         public HttpResponseMessage DeleteService(int id)   
    39.         {  
    40.             serviceData.RemoveAt(id);  
    41.             return Request.CreateResponse(HttpStatusCode.OK, "Item Deleted Successfully");  
    42.         }  
    43.     }  
    44. }  

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

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.

    byid

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.

    post

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.

    put

DELETE

  • Select ‘DELETE’ as method and add index for which you want to delete from the list in URL.
  • Press ‘SEND’ button.

    delete