Web API With AJAX: Understand AcceptVerb Attribute in Web API

This article explains the AcceptVerb attribute of the Web API. AcceptVerb is one attribute of the Web API actions. We can use it to allow a specific HTTP verb in a Web API action.

This is the "Web API with AJAX" article series. In this article series we are talking about AJAX operations using the Web API. We have explained various concepts and you can read them here:
This article explains the "AcceptVerb" attribute of the Web API. AcceptVerb is one attribute of the Web API actions. We can use it to allow a specific HTTP verb in a Web API action. Have a look at the following example.
 
Allow POST and GET verb in single action
 
In this example we will allow both of the GET and POST verbs to do certain actions in the Web API. Here is a sample presentation for our client application.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="APICall.aspx.cs" Inherits="WebApplication1.APICall" %>  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head runat="server">  
  4.     <script src="jquery-1.7.1.js" type="text/javascript"></script>  
  5.      <script>  
  6.          $(document).ready(function () {  
  7.              $("#Save").click(function () {  
  8.                  $.ajax({  
  9.                      url: 'http://localhost:3413/api/person',  
  10.                      type: 'POST',  
  11.                      dataType: 'json',  
  12.                      success: function (data, textStatus, xhr) {  
  13.                          console.log(data);  
  14.                      },  
  15.                      error: function (xhr, textStatus, errorThrown) {  
  16.                          console.log(textStatus);  
  17.                      }  
  18.                  });  
  19.              });  
  20.          });  
  21.     </script>  
  22. </head>  
  23. <body>  
  24.         <input type="button" id="Save" value="Save Data" />  
  25. </body>  
  26. </html>  
Here is the code of the Web API.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Collections.Specialized;  
  4. using System.Linq;  
  5. using System.Net;  
  6. using System.Net.Http;  
  7. using System.Net.Http.Formatting;  
  8. using System.Web.Http;  
  9. namespace WebApplication1.WebAPI  
  10. {  
  11.     public class personController : ApiController  
  12.     {  
  13.         [ActionName("MultiAction")]  
  14.         [AcceptVerbs("POST","GET")]  
  15.         public string MultiRequestHAndler()  
  16.         {  
  17.             return "I suppoer both POST and GET";  
  18.         }  
  19.     }  
  20. }  
We are allowing POST and GET attributes to the "MultiRequestHandler" action. Here is sample output from calling this action using the POST method.
 
 
 
Don't define more than one type action within same controller
 
In this example we will try to define more than one Get() function within the same controller. Since it is not allowed to define the same action (maybe GET or POST) more than one time, it will throw an error.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Collections.Specialized;  
  4. using System.Linq;  
  5. using System.Net;  
  6. using System.Net.Http;  
  7. using System.Net.Http.Formatting;  
  8. using System.Web.Http;  
  9. namespace WebApplication1.WebAPI  
  10. {  
  11.     public class personController : ApiController  
  12.     {  
  13.         [HttpGet]  
  14.         public string Get()  
  15.         {  
  16.             return "Another Get mthod";  
  17.         }  
  18.         [ActionName("MultiAction")]  
  19.         [AcceptVerbs("POST","GET")]  
  20.         public string MultiRequestHAndler()  
  21.         {  
  22.             return "I suppoer both POST and GET";  
  23.         }  
  24.     }  
  25. }  
At first we defined the Get() function and then we specified the GET verb for the AcceptVerbs attributes. So, basically we are defining a Post() method twice. (We are not specifying any different parameter within the Post() method.) Now, when we call this action we will encounter the following error:
 
 
 
That is due to the double definition of the Post() action. In the following example we have removed the first Post() action and modified the existing Web API.
 
Here is the modified code of the previous example.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Collections.Specialized;  
  4. using System.Linq;  
  5. using System.Net;  
  6. using System.Net.Http;  
  7. using System.Net.Http.Formatting;  
  8. using System.Web.Http;  
  9. namespace WebApplication1.WebAPI  
  10. {  
  11.     public class personController : ApiController  
  12.     {  
  13.         [ActionName("MultiAction")]  
  14.         [AcceptVerbs("POST","GET")]  
  15.         public string MultiRequestHAndler()  
  16.         {  
  17.             return "I suppoer both POST and GET";  
  18.         }  
  19.     }  
  20. }  
Now we are seeing that the action has executed from the client application. Here we are halting the application to the observer in a better way.
 
Conclusion
 
In this article we have learned about the "AcceptVerb" attribute with a relevant example. In a future article we will learn more about the Web API. Happy programming.