What is the difference between [Acceptverbs(Httpverbs.Get)] and [Httpget]?

Asked

Viewed 563 times

3

What good is HttpGet and what’s the point AcceptVerbs(HttpVerbs.Get) ?

Example:

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

Other Example:

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

What are the functionalities between them ?

1 answer

2


There is no difference in this two snippets of code, they have the same purpose, so that the method only accepts requests from verb GET, in fact one is abbreviation of the other. In the code source of Httpgetattribute it is quite 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 authorship rights Copyright (c) Microsoft Open Technologies, Inc. All Rights reserved in the link


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

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

or

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

thus the method accepts requests from verb GET or POST in the example.

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

References:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.