用于Action的MVC [HttpPost/HttpGet]

Nat*_*Pet 52 model-view-controller asp.net-mvc

我正在使用MVC C#.

有人可以举例说明为什么会使用它

[HttpPost/HttpGet] 
Run Code Online (Sandbox Code Playgroud)

一个行动.一个活跃的两者如何兼具 - 实际用途是什么?

Jes*_*lam 84

假设您有一个Login为用户提供登录屏幕的操作,然后在用户提交表单后收回用户名和密码:

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

public ActionResult Login(string userName, string password) {
    // do login stuff
    return View();
}
Run Code Online (Sandbox Code Playgroud)

即使我们可以通过查看它来告诉MVC,也没有明确说明哪个动作是哪个动作.如果你将[HttpGet]添加到第一个动作并将[HttpPost]添加到section动作中,MVC清楚地知道哪个动作是哪个动作.

为什么?请参阅请求方法.长和短:当用户查看页面时,这是一个GET请求,当用户提交表单时,通常是一个POST请求.HttpGet和HttpPost只是将操作限制为适用的请求类型.

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

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

如果您的操作处理来自多个谓词的请求,您还可以组合请求方法属性:

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

  • 刚试过MVC4.[HttpGet] [HttpPost]不起作用,只接受GET请求(或者先写入动词).但是,[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]确实运行良好. (3认同)

drz*_*aus 21

您不需要同时指定两者,除非您专门限制其他动词(即您不想要PUT或DELETE等).

与一些评论相反,我也无法同时使用这两个属性[HttpGet, HttpPost],但能够指定两个动词.

操作

    private ActionResult testResult(int id)
    {
        return Json(new {
                            // user input
                            input = id,
                            // just so there's different content in the response
                            when = DateTime.Now,
                            // type of request
                            req = this.Request.HttpMethod,
                            // differentiate calls in response, for matching up
                            call = new StackTrace().GetFrame(1).GetMethod().Name
                        },
                        JsonRequestBehavior.AllowGet);
    }
    public ActionResult Test(int id)
    {
        return testResult(id);
    }
    [HttpGet]
    public ActionResult TestGetOnly(int id)
    {
        return testResult(id);
    }
    [HttpPost]
    public ActionResult TestPostOnly(int id)
    {
        return testResult(id);
    }
    [HttpPost, HttpGet]
    public ActionResult TestBoth(int id)
    {
        return testResult(id);
    }
    [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
    public ActionResult TestVerbs(int id)
    {
        return testResult(id);
    }
Run Code Online (Sandbox Code Playgroud)

结果

通过POSTMAN,由markdowntables格式化

| Method    | URL                   | Response                                                                                  |
|--------   |---------------------- |----------------------------------------------------------------------------------------   |
| GET       | /ctrl/test/5          | { "input": 5, "when": "/Date(1408041216116)/", "req": "GET", "call": "Test" }             |
| POST      | /ctrl/test/5          | { "input": 5, "when": "/Date(1408041227561)/", "req": "POST", "call": "Test" }            |
| PUT       | /ctrl/test/5          | { "input": 5, "when": "/Date(1408041252646)/", "req": "PUT", "call": "Test" }             |
| GET       | /ctrl/testgetonly/5   | { "input": 5, "when": "/Date(1408041335907)/", "req": "GET", "call": "TestGetOnly" }      |
| POST      | /ctrl/testgetonly/5   | 404                                                                                       |
| PUT       | /ctrl/testgetonly/5   | 404                                                                                       |
| GET       | /ctrl/TestPostOnly/5  | 404                                                                                       |
| POST      | /ctrl/TestPostOnly/5  | { "input": 5, "when": "/Date(1408041464096)/", "req": "POST", "call": "TestPostOnly" }    |
| PUT       | /ctrl/TestPostOnly/5  | 404                                                                                       |
| GET       | /ctrl/TestBoth/5      | 404                                                                                       |
| POST      | /ctrl/TestBoth/5      | 404                                                                                       |
| PUT       | /ctrl/TestBoth/5      | 404                                                                                       |
| GET       | /ctrl/TestVerbs/5     | { "input": 5, "when": "/Date(1408041709606)/", "req": "GET", "call": "TestVerbs" }        |
| POST      | /ctrl/TestVerbs/5     | { "input": 5, "when": "/Date(1408041831549)/", "req": "POST", "call": "TestVerbs" }       |
| PUT       | /ctrl/TestVerbs/5     | 404                                                                                       |
Run Code Online (Sandbox Code Playgroud)


Ben*_*son 8

在Mvc 4中你可以使用AcceptVerbsAttribute,我认为这是一个非常干净的解决方案

[AcceptVerbs(WebRequestMethods.Http.Get, WebRequestMethods.Http.Post)]
public IHttpActionResult Login()
{
   // Login logic
}
Run Code Online (Sandbox Code Playgroud)


dkn*_*ack 5

你不能将它与属性结合起来.

但是您可以将两者放在一个操作方法上,但是您可以将逻辑封装到另一个方法中,并从这两个操作中调用此方法.

ActionName属性允许有2个ActionMethods具有相同的名称.

[HttpGet]
public ActionResult MyMethod()
{
    return MyMethodHandler();
}

[HttpPost]
[ActionName("MyMethod")]
public ActionResult MyMethodPost()
{
    return MyMethodHandler();
}

private ActionResult MyMethodHandler()
{
    // handle the get or post request
    return View("MyMethod");
}
Run Code Online (Sandbox Code Playgroud)

  • 不确定"你不能把它与属性结合起来." 意味着,但您可以组合属性,如另一个答案所示. (3认同)