Web API With AJAX: Understand Method Name and Attribute in Web API

This article explains action names and various attributes of them as well as the behavior actions corresonding to various attributes.

This is the "Web API with AJAX" article series. This series is explaining various important concepts related to the Web API and AJAX operations. You can visit our previous explanations here:
This article explains action names and various attributes of them as well as the behavior actions corresonding to various attributes.
 
Action name does not make sense
 
Let's start with one of my small actual experiences. When I was just beginning to learn the Web API , one of my colleagues (from whom I had learned the term Web API) told me that at time of a create action in the Web API we need to provide a matching name with a HTTP verb. How? If we specify a HttpPost attribute on any action then we need to provide the action name as Post(). But the concept is totally wrong. We can choose an action name of our own. In the following example we have specified the HttpGet() attribute and chosen Post() as the action name and obviously it will work smoothly. Here is the implementation of the client code.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="APICall.aspx.cs" Inherits="WebApplication1.APICall" %>  
  2. <head runat="server">  
  3.     <script src="jquery-1.7.1.js" type="text/javascript"></script>  
  4.      <script>  
  5.          $(document).ready(function () {   
  6.              $("#Save").click(function () {  
  7.                  $.ajax({  
  8.                      url: 'http://localhost:3413/api/person',  
  9.                      type: 'GET',  
  10.                      dataType: 'json',  
  11.                      success: function (data, textStatus, xhr) {  
  12.                          console.log(data);  
  13.                      },  
  14.                      error: function (xhr, textStatus, errorThrown) {  
  15.                          console.log('Error in Operation');  
  16.                      }  
  17.                  });  
  18.              });  
  19.          });  
  20.     </script>  
  21. </head>  
  22. <body>  
  23.      <input type="button" id="Save" value="Save Data" />  
  24. </body>  
  25. </html>  
We are seeing that we are making a GET request to the ajax() function. Here is the implementation of the Web API and we are seeing that the HttpGet attribute is specified on top of the Post() action.
  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 WebApplication1.WebAPI  
  9. {  
  10.     public class personController : ApiController  
  11.    {  
  12.         [HttpGet]  
  13.         public String Post()  
  14.         {  
  15.             return "Post method in HttpGet attribute";  
  16.         }  
  17.     }  
  18. }  
Here is the output of the above example.
 
 
Not even for a Get() and Post() are we allowed to provide any name as the action name. Here we have chosen "PostAction" as the action name.
  1. public class personController : ApiController  
  2. {  
  3.     [HttpGet]  
  4.     public String PostAction()  
  5.     {  
  6.          return "Post method in Diffrent Name";  
  7.     }  
  8. }  
Use ActionName attribute
 
We can use the "ActionName" attribute to specify any name for a specific action. In this example we will see how to implement it. Here is an implementation of the client code.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="APICall.aspx.cs" Inherits="WebApplication1.APICall" %>  
  2. <head runat="server">  
  3.     <script src="jquery-1.7.1.js" type="text/javascript"></script>  
  4.      <script>  
  5.          $(document).ready(function () {   
  6.              $("#Save").click(function () {  
  7.                  $.ajax({  
  8.                      url: 'http://localhost:3413/api/person/MyPostAction',  
  9.                      type: 'GET',  
  10.                      dataType: 'json',  
  11.                      success: function (data, textStatus, xhr) {  
  12.                          console.log(data);  
  13.                      },  
  14.                      error: function (xhr, textStatus, errorThrown) {  
  15.                          console.log('Error in Operation');  
  16.                      }  
  17.                  });  
  18.              });  
  19.          });  
  20.     </script>  
  21. </head>  
  22. <body>  
  23.     <form name="form1" id="form1">  
  24.         <input type="button" id="Save" value="Save Data" />  
  25.     </form>  
  26. </body>  
  27. </html>  
We are seeing that the phrase "MyPostAction" is added at the very last to the URL. Here is the implementation of the Web API.
  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 WebApplication1.WebAPI  
  9. {   
  10.     public class personController : ApiController  
  11.     {  
  12.         [HttpGet]  
  13.         [ActionName("MyPostAction")]  
  14.         public String GetAction()  
  15.         {  
  16.             return "Get Method with Action Name";  
  17.         }   
  18.     }  
  19. }   
We are specifying the "ActionName" attribute on top of the GetAction(). Here is sample output.
 
 
NonAction attribute to prevent call from client
 
We can specify the NonAction attribute on top of any action that implies that the action cannot be accessed from the client part. In this example we will try to call "MyPostAction".
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="APICall.aspx.cs" Inherits="WebApplication1.APICall" %>  
  2. <head runat="server">  
  3.     <script src="jquery-1.7.1.js" type="text/javascript"></script>  
  4.      <script>  
  5.          $(document).ready(function () {   
  6.              $("#Save").click(function () {  
  7.                  $.ajax({  
  8.                      url: 'http://localhost:3413/api/person/MyPostAction',  
  9.                      type: 'GET',  
  10.                      dataType: 'json',  
  11.                      success: function (data, textStatus, xhr) {  
  12.                          console.log(data);  
  13.                      },  
  14.                      error: function (xhr, textStatus, errorThrown) {  
  15.                          console.log('Error in Operation');  
  16.                      }  
  17.                  });  
  18.              });  
  19.          });  
  20.     </script>  
  21. </head>  
  22. <body>  
  23.     <input type="button" id="Save" value="Save Data" />  
  24. </body>  
  25. </html>  
We are seeing that the "MyPostAction" is specified with the NonAction attribute. So, it's not possible to call it from an external application.
  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 WebApplication1.WebAPI  
  9. {   
  10.     public class personController : ApiController  
  11.     {  
  12.         [HttpGet]  
  13.         [NonAction]  
  14.         [ActionName("MyPostAction")]  
  15.         public String GetAction()  
  16.         {  
  17.             return "Get Method with Action Name";  
  18.         }   
  19.     }  
  20. }   
We are experiencing an error at the time of an action call.
 
 
ActiveVerb attribute to restrict action for specific verbs
 
We can use the "ActiveVerb" attribute to allow certain verbs to consume a service from a certain action. In the following example we are allowing a POST verb to certain controller and we are trying to consume it using the GET verb. Have a look at the following example.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="APICall.aspx.cs" Inherits="WebApplication1.APICall" %>  
  2. <!DOCTYPE html>  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  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: 'GET',  
  11.                      dataType: 'json',  
  12.                      success: function (data, textStatus, xhr) {  
  13.                          console.log(data);  
  14.                      },  
  15.                      error: function (xhr, textStatus, errorThrown) {  
  16.                          console.log('Error in Operation');  
  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 implementation of the Web API.
  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 WebApplication1.WebAPI  
  9. {   
  10.     public class personController : ApiController  
  11.     {         
  12.         [AcceptVerbs("POST")]  
  13.         public String GetAction()  
  14.         {  
  15.             return "Get Method with Action Name";  
  16.         }  
  17.     }  
  18. }  
We are seeing that the GET request cannot consume the GetAction() action because it only accepts a POST verb.
 
 
Allow multiple verbs in single action
 
We can allow multiple verbs to single action. In this example we are allowing the GET and POST verbs to the GetAction controller.
  1. public class personController : ApiController  
  2. {  
  3.     [AcceptVerbs("GET","POST")];  
  4.     public String GetAction()  
  5.     {  
  6.         return "Get Method with Action Name";  
  7.     }  
  8. }   
Conclusion
 
In this article we have learned various attributes of actions in the Web API. It will be very helpful in daily programming life. Hope you get some new ideas from this article.