asp.net-mvc-5 – What is the difference between [AcceptVerbs(HttpVerbs.Get)] and [HttpGet]?

Question:

What is HttpGet and what is AcceptVerbs(HttpVerbs.Get) ?

Example:

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult GetStatesByCountryId(string countryId)
{
    return ....
}

Another example:

[HttpGet]
public ActionResult GetStatesByCountryId(string countryId)
{
    return ....
}

What is the functionality between them?

Answer:

There is no difference in these two pieces of code, they have the same purpose, for the method only accepts GET verb requests, in fact one is an abbreviation for the other. In the source code of the HttpGetAttribute it is very clear that it is the abbreviation for AcceptVerbsAttribute :

HttpGetAttribute Code :

using System.Reflection;

namespace System.Web.Mvc
{
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
    public sealed class HttpGetAttribute : ActionMethodSelectorAttribute
    {
        private static readonly AcceptVerbsAttribute _innerAttribute = 
                                        new AcceptVerbsAttribute(HttpVerbs.Get);

        public override bool IsValidForRequest(ControllerContext controllerContext, 
                                               MethodInfo methodInfo)
        {
            return _innerAttribute
                       .IsValidForRequest(controllerContext, methodInfo);
        }
    }
}

Source and Copyright Copyright (c) Microsoft Open Technologies, Inc. All rights reserved at link


An advantage of using the AcceptVerbs() attribute is that it can contain more than one configured verb type, for example :

[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]

Where

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

therefore, the method accepts GET or POST verb requests in the example.

Notes: Remember that if there is no configuration ( [HttpGet] , [HttpPost] or [AcceptVerbs("POST")] , etc.) the method accepts by default the GET verb

References:

Scroll to Top