且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MVC [HttpPost / HTTPGET]行动

更新时间:2022-11-27 22:01:03

让我们假设你有一个登录操作提供一个登录屏幕的用户,然后接收用户名和密码的用户回来后提交表单:

Let's say you have a Login action which provides the user with a login screen, then receives the user name and password back after the user submits the form:

public ActionResult Login() {
    return View();
}

public ActionResult Login(string userName, string password) {
    // do login stuff
    return View();
}

MVC未给出明确的指示,在该诉讼是,尽管我们可以通过看它告诉。如果添加[HTTPGET]在第一个动作和[HttpPost]的部分动作,显然MVC知道哪些动作是哪个。

MVC isn't being given clear instructions on which action is which, even though we can tell by looking at it. If you add [HttpGet] to the first action and [HttpPost] to the section action, MVC clearly knows which action is which.

为什么呢?请参见请求方法的。多空:当用户查看网页,这是一个GET请求,当用户提交一个表单,这通常是一个POST请求。 HTTPGET和HttpPost只是限制措施适用的请求类型。

Why? See Request Methods. Long and short: When a user views a page, that's a GET request and when a user submits a form, that's usually a POST request. HttpGet and HttpPost just restrict the action to the applicable request type.

[HttpGet]
public ActionResult Login() {
    return View();
}

[HttpPost]
public ActionResult Login(string userName, string password) {
    // do login stuff
    return View();
}

您还可以结合你的动作提供来自多个谓词的请求请求方法属性:

You can also combine the request method attributes if your action serves requests from multiple verbs:

的[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]