Web API With AJAX: Understand AcceptVerb Attribute in Web API

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.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="APICall.aspx.cs" Inherits="WebApplication1.APICall" %>

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <script src="jquery-1.7.1.js" type="text/javascript"></script>

     <script>

         $(document).ready(function () {

             $("#Save").click(function () {

                 $.ajax({

                     url: 'http://localhost:3413/api/person',

                     type: 'POST',

                     dataType: 'json',

                     success: function (data, textStatus, xhr) {

                         console.log(data);

                     },

                     error: function (xhr, textStatus, errorThrown) {

                         console.log(textStatus);

                     }

                 });

 

             });

         });

    </script>

</head>

<body>

        <input type="button" id="Save" value="Save Data" />

</body>

</html>

Here is the code of the Web API.
 

using System;

using System.Collections.Generic;

using System.Collections.Specialized;

using System.Linq;

using System.Net;

using System.Net.Http;

using System.Net.Http.Formatting;

using System.Web.Http;

 

namespace WebApplication1.WebAPI

{

    public class personController : ApiController

    {

        [ActionName("MultiAction")]

        [AcceptVerbs("POST","GET")]

        public string MultiRequestHAndler()

        {

            return "I suppoer both POST and GET";

        }

 

    }

}

We are allowing POST and GET attributes to the "MultiRequestHandler" action. Here is sample output from calling this action using the POST method.

Allow POST and GET verb in single action

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.
 

using System;

using System.Collections.Generic;

using System.Collections.Specialized;

using System.Linq;

using System.Net;

using System.Net.Http;

using System.Net.Http.Formatting;

using System.Web.Http;

 

namespace WebApplication1.WebAPI

{

    public class personController : ApiController

    {

        [HttpGet]

        public string Get()

        {

            return "Another Get mthod";

        }

 

        [ActionName("MultiAction")]

        [AcceptVerbs("POST","GET")]

        public string MultiRequestHAndler()

        {

            return "I suppoer both POST and GET";

        }

 

    }

}

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:

Allow POST and GET verb in single action1

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.

using System;

using System.Collections.Generic;

using System.Collections.Specialized;

using System.Linq;

using System.Net;

using System.Net.Http;

using System.Net.Http.Formatting;

using System.Web.Http;

 

namespace WebApplication1.WebAPI

{

    public class personController : ApiController

    {

 

        [ActionName("MultiAction")]

        [AcceptVerbs("POST","GET")]

        public string MultiRequestHAndler()

        {

            return "I suppoer both POST and GET";

        }

    }

}

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.
 

Up Next
    Ebook Download
    View all
    Learn
    View all