cod*_*tte 55 asp.net-mvc post controller get asp-net-mvc-1
在我的控制器中,我有两个叫做"朋友"的动作.执行的那个取决于它是否是"获取"而不是"帖子".
所以我的代码片段看起来像这样:
// Get:
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Friends()
{
// do some stuff
return View();
}
// Post:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Friends()
{
// do some stuff
return View();
}
Run Code Online (Sandbox Code Playgroud)
但是,这不会编译,因为我有两个具有相同签名的方法(Friends).我该怎么做呢?我是否只需要创建一个操作但区分其中的"获取"和"发布"?如果是这样,我该怎么做?
Çağ*_*kin 119
将第二种方法重命名为"Friends_Post",然后您可以将[ActionName("Friends")]属性添加到第二种方法.因此,使用POST作为请求类型的Friend操作请求将由该操作处理.
// Get:
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Friends()
{
// do some stuff
return View();
}
// Post:
[ActionName("Friends")]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Friends_Post()
{
// do some stuff
return View();
}
Run Code Online (Sandbox Code Playgroud)
moh*_*tan 20
如果你真的只想要一个例程来处理这两个动词,试试这个:
[AcceptVerbs("Get", "Post")]
public ActionResult ActionName(string param1, ...)
{
//Fun stuff goes here.
}
Run Code Online (Sandbox Code Playgroud)
一个潜在的警告:我正在使用MVC版本2.不确定这是否在MVC 1中得到支持.InternetVerbs的Intellisense文档应该让你知道.
尝试使用:
[AcceptVerbs(HttpVerbs.Post | HttpVerbs.Get)]
public ActionResult Friends()
{
// do some stuff
return View();
}
Run Code Online (Sandbox Code Playgroud)