MVC [HttpPost / HTTPGET]行动 [英] MVC [HttpPost/HttpGet] for Action

查看:264
本文介绍了MVC [HttpPost / HTTPGET]行动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MVC C#。

有人能给为什么人会用一个例子

Can somebody give an example on why one would use

[HttpPost/HttpGet] 

有关的行动。积极怎样才能兼得 - 有什么实际用途?

for an Action. How can an active have both - what is the practical use?

推荐答案

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

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

这篇关于MVC [HttpPost / HTTPGET]行动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆