1

Is it possible to have an AsyncController that has a GET and POST action of the same name?

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();
    }
}

When I tried this I got an error:

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

Is it possible to have them co-exist in any way or does every asynchronous action method have to be unique?

Dismissile
  • 32,564
  • 38
  • 174
  • 263
  • See, http://stackoverflow.com/questions/4432653/async-get-post-and-action-name-conflicts-in-asp-net-mvc – Chris Chilvers Jan 05 '12 at 16:31
  • I'm not sure it makes sense to have [HttpPost] decorations on the *Completed methods. Aren't those called internally by the controller? If so, they shouldn't have to a POST. – 3Dave Jan 05 '12 at 16:34

1 Answers1

0

You can have the multiple IndexAsync methods, but only the one IndexCompleted method eg:

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();
  }
}

(Only the attributes on the MethodNameAsync methods are used by MVC, so are not required on the MethodNameCompleted methods)

Rich O'Kelly
  • 41,274
  • 9
  • 83
  • 114
  • Is there a way to differentiate between which Async method is being completed, so I would know to return View A vs View B, etc? – Dismissile Jan 05 '12 at 16:59
  • @Dismissile You could pass the viewName as a parameter (eg `AsyncManager.Parameters["viewName"] = "ViewA";`) or alternatively check the HttpRequestBase.HttpMethod property. – Rich O'Kelly Jan 06 '12 at 10:12