GET和POST到ASP中的相同的控制器操作。NET MVC

时间:2022-12-01 08:16:22

I'd like to have a single action respond to both Gets as well as Posts. I tried the following

我想让一个动作同时对get和post做出响应。我尝试以下

[HttpGet]
[HttpPost]
public ActionResult SignIn()

That didn't seem to work. Any suggestions ?

这似乎行不通。有什么建议吗?

3 个解决方案

#1


117  

This is possible using the AcceptVerbs attribute. Its a bit more verbose but more flexible.

这是可以使用accept动词属性的。它有点冗长,但更灵活。

[AcceptVerbs(HttpVerbs.Get|HttpVerbs.Post)]
public ActionResult SignIn()
{
}

More on msdn.

msdn。

#2


59  

Actions respond to both GETs and POSTs by default, so you don't have to specify anything:

默认情况下,动作响应get和post,因此您不必指定任何内容:

public ActionResult SignIn()
{
    //how'd we get here?
    string method = HttpContext.Request.HttpMethod;
    return View();
}

Depending on your need you could still perform different logic depending on the HttpMethod by operating on the HttpContext.Request.HttpMethod value.

根据您的需要,您仍然可以通过对HttpContext.Request进行操作来根据HttpMethod执行不同的逻辑。HttpMethod价值。

#3


0  

[HttpGet]
public ActionResult SignIn()
{
}

[HttpPost]
public ActionResult SignIn(FormCollection form)
{
}

#1


117  

This is possible using the AcceptVerbs attribute. Its a bit more verbose but more flexible.

这是可以使用accept动词属性的。它有点冗长,但更灵活。

[AcceptVerbs(HttpVerbs.Get|HttpVerbs.Post)]
public ActionResult SignIn()
{
}

More on msdn.

msdn。

#2


59  

Actions respond to both GETs and POSTs by default, so you don't have to specify anything:

默认情况下,动作响应get和post,因此您不必指定任何内容:

public ActionResult SignIn()
{
    //how'd we get here?
    string method = HttpContext.Request.HttpMethod;
    return View();
}

Depending on your need you could still perform different logic depending on the HttpMethod by operating on the HttpContext.Request.HttpMethod value.

根据您的需要,您仍然可以通过对HttpContext.Request进行操作来根据HttpMethod执行不同的逻辑。HttpMethod价值。

#3


0  

[HttpGet]
public ActionResult SignIn()
{
}

[HttpPost]
public ActionResult SignIn(FormCollection form)
{
}