81

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?

Lorenzo
  • 28,103
  • 43
  • 117
  • 208

2 Answers2

205

[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)].

Nick Butler
  • 22,781
  • 4
  • 45
  • 70
Rudresha Parameshappa
  • 3,514
  • 3
  • 24
  • 32
  • 20
    this is more correct and informative answer than accepted one. – 24x7Programmer Sep 19 '13 at 17:49
  • 1
    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) – SerjG Jul 14 '14 at 22:16
  • 1
    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. http://stackoverflow.com/questions/16658020/public-action-method-was-not-found-on-controller – Tony Wall Apr 02 '15 at 06:55
  • 2
    @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). – Robert Harvey Jun 30 '15 at 19:15
  • @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". – Tony Wall Jul 01 '15 at 20:24
  • [AcceptVerbs("GET" , "POST")] – Kiran Solkar Jul 17 '17 at 09:13
55

Nothing. One is just shorthand for the other.

Matthew Manela
  • 16,194
  • 3
  • 61
  • 64