What is the difference between [AcceptVerbs (HttpVerbs.Get)] and [HttpGet]?

3

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 are the features between them?

    
asked by anonymous 26.12.2016 / 20:06

1 answer

2

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

Code < HttpGetAttribute :

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

One advantage of using GET is that it can contain more than one type of AcceptVerbs() , example :

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

or

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

so the method accepts requests from verb verb or GET in the example

Note that if there is no configuration ( POST , [HttpGet] or [HttpPost] , etc.) the method accepts by

Remarks: standard [AcceptVerbs("POST")] verb

References:

26.12.2016 / 20:10