ASP.NET MVC 2 introduces asynchronous controllers. An asynchronous controller is just a variation of a standard ASP.NET MVC controller that sets up an asynchronous interaction with the surrounding ASP.NET runtime. In a way, asynchronous controllers are the ASP.NET MVC counterpart of asynchronous pages that we have in Web Forms.
This post touches the ASP.NET MVC async controller. Its included in the futures library and is available on codeplex. This may become part of ASP.NET MVC 2. Download and add reference to the "Microsoft.Web.Mvc" dll. Make the following changes in the Global.asax to set the rounte to handle async request.
In the above route the "Index" action on the "Home" controller is mapped to async route. Note the "MapAsyncRoute" for sync calls you use MapRoute(..).
This post touches the ASP.NET MVC async controller. Its included in the futures library and is available on codeplex. This may become part of ASP.NET MVC 2. Download and add reference to the "Microsoft.Web.Mvc" dll. Make the following changes in the Global.asax to set the rounte to handle async request.
In the above route the "Index" action on the "Home" controller is mapped to async route. Note the "MapAsyncRoute" for sync calls you use MapRoute(..).
Have a look at the controller.
namespace AsynPortal.Controllers
{
public class HomeController : AsyncController
{
// The async framework will call this first when it matches the route
public void Index()
{
// Set a default value for our result param
// (will be passed to the MyActionCompleted method below)
namespace AsynPortal.Controllers
{
public class HomeController : AsyncController
{
// The async framework will call this first when it matches the route
public void Index()
{
// Set a default value for our result param
// (will be passed to the MyActionCompleted method below)
// Indicate that we're performing an operation we want to offload
AsyncManager.OutstandingOperations.Increment(2);
var client = new WebClient();
client.DownloadStringCompleted += (s, e) =>
{
if (!e.Cancelled && e.Error == null)
{
// We were successful, set the result
AsyncManager.Parameters["site1"] = e.Result;
}
// Indicate that we've completed the offloaded operation
AsyncManager.OutstandingOperations.Decrement();
};
{
if (!e.Cancelled && e.Error == null)
{
// We were successful, set the result
AsyncManager.Parameters["site1"] = e.Result;
}
// Indicate that we've completed the offloaded operation
AsyncManager.OutstandingOperations.Decrement();
};
// Actually start the download
client.DownloadStringAsync(new Uri("http://geekswithblogs.net/rajeshpillai"));
client.DownloadStringAsync(new Uri("http://geekswithblogs.net/rajeshpillai"));
var client1 = new WebClient();
client1.DownloadStringCompleted += (s, e) =>
{
if (!e.Cancelled && e.Error == null)
{
// We were successful, set the result
AsyncManager.Parameters["site2"] = e.Result;
}
{
if (!e.Cancelled && e.Error == null)
{
// We were successful, set the result
AsyncManager.Parameters["site2"] = e.Result;
}
// Indicate that we've completed the offloaded operation
AsyncManager.OutstandingOperations.Decrement();
};
AsyncManager.OutstandingOperations.Decrement();
};
// Actually start the download
client1.DownloadStringAsync(new Uri("http://www.google.com"));
}
client1.DownloadStringAsync(new Uri("http://www.google.com"));
}
// This will be called when the outstanding operation(s) have completed
public ActionResult IndexCompleted(string site1, string site2)
{
return View("Index", new PortalViewModel
{
Site1 = site1,
Site2 = site2
});
}
}
}
The above Index() method sets up two asynchronous request. Evern async method has its "actioncompleted" method also. In the above case it is "IndexCompleted" method.
{
return View("Index", new PortalViewModel
{
Site1 = site1,
Site2 = site2
});
}
}
}
The above Index() method sets up two asynchronous request. Evern async method has its "actioncompleted" method also. In the above case it is "IndexCompleted" method.
The line AsyncManager.OutstandingOperations.Increment(2) sets that two operations will be running in async mode. The IndexCompleted action method is invoked once all the async method is completed execution. The parameters are passed to the IndexCompleted method by
AsyncManager.Parameters["site2"] = e.Result;
To indicate that the method completed execution call the
AsyncManager.OutstandingOperations.Decrement() method.
The model is declared below.
namespace Demo.Model
{
public class PortalViewModel
{
public string Site1 { get; set; }
public string Site2 { get; set; }
}
}
The model is declared below.
namespace Demo.Model
{
public class PortalViewModel
{
public string Site1 { get; set; }
public string Site2 { get; set; }
}
}
Hope this post can help you.

