1

I want to use the HttpGet and HttpPost attributes for one action method. However, I have only seen examples where the attributes are used individually on separate action methods.

For example:

public ActionResult Index()
    {
        //Code...
        return View();
    }

    [HttpPost]
    public ActionResult Index(FormCollection form)
    {
        //Code...
        return View();
    }

I want to have something like this:

[HttpGet][HttpPost]
public ActionResult Index(FormCollection form)
{
    //Code...
    return View();
}

I remember having seen this done somewhere, but cannot remember where.

Jed
  • 10,649
  • 19
  • 81
  • 125
Christian
  • 117
  • 1
  • 12

1 Answers1

3

If you really want to do that, you can use the [AcceptVerbs] attribute. (See this SO question)

This way your method can handle the GET and POST verbs (but not others like PUT)

[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
public ActionResult Index(FormCollection form)
{
    //Code...
    return View();
}

If you want your method to handle all verbs, don´t use any attribute at all:

public ActionResult Index(FormCollection form)
{
    //Code...
    return View();
}
Community
  • 1
  • 1
Daniel J.G.
  • 34,266
  • 9
  • 112
  • 112