1

是否可以有一个具有相同名称的 GET 和 POST 操作的 AsyncController?

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

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

4

1 回答 1

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

于 2012-01-05T16:25:52.407 回答