Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Asynchronous Action in ASP.NET MVC Futures

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about how to use asynchronous Action in ASP.NET MVC Futures. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Using asynchrony in ASP.NET MVC is troublesome, and several classes that support asynchrony have been provided in ASP.NET MVC Futures since the RC1 version.

Related classes are: AsyncActionDescriptor, AsyncController, AsyncControllerActionInvoker, AsyncManager, AsyncResultWrapper, AsyncTimeoutAttribute, NoAsyncTimeoutAttribute.

The related APIs are: IAsyncActionDescriptor, IAsyncActionInvoker, IAsyncController, IAsyncManagerContainer.

Let's explain how to use them.

1. Preparation before using asynchronous Action

1. Reference Microsoft.Web.Mvc.

two。 First, the Url to be processed asynchronously is handed over to MvcHttpAsyncHandler. In this step, AsyncRouteCollectionExtensions.MapAsyncRoute can set the rules and change the rules of the original MapRoute processing to MapAsyncRoute, such as:

Routes.MapAsyncRoute ("Default", "{controller} / {action} / {id}", new {controller = "Home", action = "Index", id = ""})

3. Inherit the corresponding Controller to AsyncController.

Public class HomeController: AsyncController {}

4. We agreed that the following defined Action are all in HomeController.

2. * Asynchronous Action methods: Action and ActionCompleted

ASP.NET MVC Futures supports the method of automatically finding asynchronous Action by name

Its usage is as follows:

Public void Async1 () {/ / main thread} public ActionResult Async1Completed () {/ / automatically find the Action with the main thread Action name + Completed as asynchronous Action return Content ("Async1");}

Third, the second asynchronous Action mode: BeginAction, EndAction mode

The second method is fine if you know about it, but it is used in conjunction with other types of asynchronous calls.

Public delegate void AsyncEventHandler (); / / A delegate is declared here, and / / the asynchronous process public void Event1 () {} public IAsyncResult BeginAsync3 (AsyncCallback callback, object state) {AsyncEventHandler asy = new AsyncEventHandler (Event1); ViewData ["a"] = asy can also be implemented using WebRequest/WebResponse/SqlConnection. / / Auxiliary storage objects must be used to transfer values between methods, and the same is true in * methods: return asy.BeginInvoke (callback, state);} public void EndAsync3 (IAsyncResult result) {/ / transfer to asynchronous Action var a = ViewData ["a"] as AsyncEventHandler; a.EndInvoke (result); Content ("complete") .ExecuteResult (this.ControllerContext);}

Fourth, the third asynchronous Action mode: using AsyncManager.RegisterTask and delegation

If you find it troublesome (and indeed troublesome) to use two methods to implement asynchronous Action, you can use AsyncManager.RegisterTask to invoke the delegate to implement the asynchronous invocation.

Public void Async2 () {this.AsyncManager.RegisterTask (c = > {/ / main thread, call asynchronous thread c (null);}, delegate (IAsyncResult result) {/ / asynchronous part Content ("Async2") .ExecuteResult (this.ControllerContext);});}

In fact, no matter which method it is, I personally think that Action/ActionCompleted 's method may be more beautiful and suitable for general use (just these three comparisons). It's just that the AsyncManager.RegisterTask method is more convenient to pass values, while the Begin/End method is more suitable for cooperating with other asynchronous operations.

The above is the editor for you to share how to use asynchronous Action in ASP.NET MVC Futures, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report