post and get with same method signature

2019-01-10 22:11发布

In my controller I have two actions called "Friends". The one that executes depends on whether or not it's a "get" versus a "post".

So my code snippets look something like this:

// Get:
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Friends()
{
    // do some stuff
    return View();
}

// Post:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Friends()
{
    // do some stuff
    return View();
}

However, this does not compile since I have two methods with the same signature (Friends). How do I go about creating this? Do I need to create just one action but differentiate between a "get" and "post" inside of it? If so, how do I do that?

7条回答
淡お忘
2楼-- · 2019-01-10 22:47

Try using:

[AcceptVerbs(HttpVerbs.Post | HttpVerbs.Get)]
public ActionResult Friends()
{
    // do some stuff
    return View();
}
查看更多
登录 后发表回答