是否可以让AsyncController具有相同名称的GET和POST操作?
public class HomeController : AsyncController
{
[HttpGet]
public void IndexAsync()
{
// ...
}
[HttpGet]
public ActionResult IndexCompleted()
{
return View();
}
[HttpPost]
public void IndexAsync(int id)
{
// ...
}
[HttpPost]
public ActionResult IndexCompleted(int id)
{
return View();
}
}
当我尝试这个时,我收到了一个错误:
Lookup for method 'IndexCompleted' on controller type 'HomeController' failed because of an ambiguity between the following methods:
System.Web.Mvc.ActionResult IndexCompleted() on type Web.Controllers.HomeController
System.Web.Mvc.ActionResult IndexCompleted(System.Int32) on type Web.Controllers.HomeController
是否可以让它们以任何方式共存,或者每个异步操作方法都必须是唯一的?
答案 0 :(得分:0)
您可以使用多个IndexAsync
方法,但只能使用一个IndexCompleted
方法,例如:
public class HomeController : AsyncController
{
[HttpGet]
public void IndexAsync()
{
AsyncManager.OutstandingOperations.Increment(1);
// ...
AsyncManager.Parameters["id"] = null;
AsyncManager.OutstandingOperations.Decrement();
// ...
}
[HttpPost]
public void IndexAsync(int id)
{
AsyncManager.OutstandingOperations.Increment(1);
// ...
AsyncManager.Parameters["id"] = id;
AsyncManager.OutstandingOperations.Decrement();
// ...
}
public ActionResult IndexCompleted(int? id)
{
return View();
}
}
(MVC仅使用MethodNameAsync
方法上的属性,因此MethodNameCompleted
方法不需要这些属性)