asp.net-mvc [AcceptVerbs(HttpVerbs.Post)] 和 [HttpPost] 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3843875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:31:41  来源:igfitidea点击:

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

asp.net-mvcasp.net-mvc-2http-posthttp-get

提问by Lorenzo

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

我可以用 [AcceptVerbs(HttpVerbs.Post)]/[AcceptVerbs(HttpVerbs.Get)] 装饰一个动作

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

or with the [HttpPost]/[HttpGet] attributes

或使用 [HttpPost]/[HttpGet] 属性

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

Are they different?

它们不同吗?

采纳答案by Matthew Manela

Nothing. One is just shorthand for the other.

没有。一个只是另一个的简写。

回答by Rudresha Parameshappa

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

[HttpPost]是 的简写[AcceptVerbs(HttpVerbs.Post)]。唯一的区别是你不能[HttpGet, HttpPost]在同一个动作上一起使用(和相似的)。如果您希望操作同时响应 GET 和 POST,则必须使用[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)].