What is the difference between [AcceptVerbs(HttpVerbs.Post)] and [HttpPost]?
Asked Answered
A

3

86

I can decorate an action either with the [AcceptVerbs(HttpVerbs.Post)]/[AcceptVerbs(HttpVerbs.Get)]

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(string title)
{
    // Do Something...
}

or with the [HttpPost]/[HttpGet] attributes

[HttpPost]
public ActionResult Create(string title)
{
    // Do Something...
}

Are they different?

Augmentative answered 2/10, 2010 at 0:39 Comment(4)
HttpPost is MVC 2.0+, and as Matthew said, its just short handStockinet
how are expressions like [HttpPost] called in asp net mvc? decorators?Menashem
@andi They are called "Attributes" msdn.microsoft.com/en-us/library/z0w1kczw.aspxGlairy
I've found that [AcceptVerbs("GET")] can stop an entry for that action being generated in the help docs.Tildi
E
56

Nothing. One is just shorthand for the other.

Elysian answered 2/10, 2010 at 0:41 Comment(2)
One does not simply accept get and post in one endpoint. What's the point of that endpoint?Critique
this should not be the suggested answer since it is wrong.Sweat
B
222

[HttpPost] is shorthand for [AcceptVerbs(HttpVerbs.Post)]. The only difference is that you can't use [HttpGet, HttpPost] (and similar) together on the same action. If you want an action to respond to both GETs and POSTs, you must use [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)].

Brackely answered 16/4, 2013 at 10:8 Comment(6)
this is more correct and informative answer than accepted one.Enfilade
I prefer to use [HttpPost] and [HttpGet]. When I need them both for one action: just don't use any (since you don't need PUT, DELETE or others)Capias
I prefer consistency, which means unfortunately only "the old" AcceptVerbs is the way which will always work, shame. Microsoft should change the attribute to allow multiple usage and process that accordingly in their pipeline, to prevent this "new" method causing confusion and trouble, e.g. #16658520Damian
@CodeChief A quick thought experiment would clarify why it's the way it is... The AcceptVerbs attribute takes a single Flags parameter. You set multiple flags by Or-ing them. [HttpPost] is merely shorthand for [AcceptVerbs(HttpVerbs.Post)] There's no mechanism available to OR flags together if you use the shorthand; that's why AcceptVerbs still exists (beyond reasons of backwards compatibility).Sharpen
@RobertHarvey - It's clear what they are, the discussion is why not allow the two different HttpGet and HttpPost attributes to work together. What I have to think of is training and building development teams. What do you tell other developers to do... "Oh use this attribute... but in this case use the other....". Hence for consistency the only one which you could tell people to use, simply, is AcceptVerbs. This limitation of HttpGet/HttpPost is not intuitive, arguably a bug. Not a big issue overall, just a "gotcha".Damian
[AcceptVerbs("GET" , "POST")]Bowstring
E
56

Nothing. One is just shorthand for the other.

Elysian answered 2/10, 2010 at 0:41 Comment(2)
One does not simply accept get and post in one endpoint. What's the point of that endpoint?Critique
this should not be the suggested answer since it is wrong.Sweat
A
0

[HttpPost] is equivalent to [AcceptVerbs(HttpVerbs.Post)] and used for Post method

[HttpGet] is equivalent to [AcceptVerbs(HttpVerbs.Get)] and used for Get method

[HttpGet, HttpPost] is equivalent to [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)] and used for both GET/POST methods

Airlee answered 19/12, 2022 at 6:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.