是否可以同时具有相同名称的GET和POST异步控制器操作?

时间:2012-01-05 16:12:38

标签: asp.net-mvc asynccontroller

是否可以让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

是否可以让它们以任何方式共存,或者每个异步操作方法都必须是唯一的?

1 个答案:

答案 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方法不需要这些属性)