Overview ASP. net mvc Framework

Source: Internet
Author: User

Asynchronous request processing is an advanced feature introduced in ASP. NET 2.0. It relies on the IO Complete Port to improve the throughput of IO-intensive applications. For details, see the principles and performance tests ). However, the ASP. net mvc framework currently lacks the asynchronous Action function, which is the "very important function that ASP. net mvc lacks" that Lao Zhao often talks about ". In the Session of TechED 2008 China, I once gave a so-called "solution", but its high complexity makes that solution too limited.

In order to make up for the regret on TechED and prepare for it. ASP. NET development conference. net mvc best practice Session, I carefully thought about this problem during the Spring Festival vacation, and got a relatively good extension: complete, convenient, it is also very lightweight-the core logic code is only about 200 lines, which means that most of the functions will be delegated to the ready-made content in the framework, ensuring the stability and efficiency of expansion and good backward compatibility.

It is worth mentioning that I am using ASP on the 1/26. the Beta version of net mvc writes the first version of this extension, and Microsoft released ASP soon.. net mvc rc. In the process of porting the solution, I found that ASP. NET MVC RC has made great improvements in the Framework Design, which has led to some changes in my policies when building extensions. Fortunately, these changes in the RC version make it easier to build an extension, especially at the "low-end" level. The ASP. net mvc framework implements its "scalable everywhere" commitment.

Now let's take a closer look at the implementation of this extension.

Change in Request Processing Method

Before formulating basic transformation strategies, we need to understand the current architecture and request processing process of ASP. net mvc framework. As follows:

When the application is started, no request is received.) register the Route policy for the MVC request to the ASP. NET Routing module. In this case, the RouteHandler attribute in each Route policy is the MvcRouteHandler in ASP. net mvc framework.

When ASP. when the NET Routing module receives an HTTP request that matches a specific Route policy, it will call the GetHttpHandler of the RouteHandler object in the Route object to obtain an HttpHandler and submit it to ASP.. NET. MvcRouteHandler will always return an MvcHandler object.

MvcHandler extracts the controller value in RouteData during execution, constructs a controller object that implements the IController interface, and calls the Execute method of the IController interface to Execute the controller.

For an ASP. net mvc application, most controllers will inherit the System. Web. Mvc. Controller type. The Controller class will get the action value from RouteData and hand it to the object implementing the IActionInvoker interface to execute an Action.

If we want to transform this process into asynchronous processing, we need to make it conform to the asynchronous Processing Method in ASP. NET architecture. The ASP. NET architecture can process asynchronous requests in several ways, such as Asynchronous pages and asynchronous Http Module. asynchronous Http Handler is the most suitable method for the current situation. To implement an asynchronous Handler, we need to implement the IHttpAsyncHandler interface for the Handler that processes the request, instead of the traditional IHttpHandler interface. The BeginProcessRequest and EndProcessRequest methods in the IHttpAsyncHandler interface constitute the APMAynchronous Programming Model and asynchronous Programming Model in. NET. You can use the "two-stage" Asynchronous call to process an HTTP request.

You should have discovered that to support asynchronous Action, you must confirm whether to execute an IHttpHandler object or IHttpAsyncHandler object based on the current request information. By default, the ASP. net mvc Framework checks, constructs, and CALLS controllers within Http Handler (MvcHandler object. This is too late. We must talk about these logics before the Routing process. Fortunately, the IRouteHandler supported by ASP. NET Routing is like IHttpHandlerFactory in ASP. NET. Different Handler can be generated as needed for execution. Therefore, we only need to build a new IRouteHandler type. So AsyncMvcRouteHandler was born. As you can imagine, some of the Code is the same as MvcHandler in the framework, to some extent, we did just advance what was originally done in MvcHandler:

  1. publicclassAsyncMvcRouteHandler:IRouteHandler  
  2. {  
  3. publicIHttpHandlerGetHttpHandler(RequestContextrequestContext)  
  4. {  
  5. stringcontrollerName=requestContext.RouteData.GetRequiredString("controller");  
  7. varfactory=ControllerBuilder.Current.GetControllerFactory();  
  8. varcontroller=factory.CreateController(requestContext,controllerName);  
  9. if(controller==null)  
  10. {  
  11. thrownewInvalidOperationException(...);  
  12. }  
  14. varcoreController=controllerasController;  
  15. if(coreController==null)  
  16. {  
  17. returnnewSyncMvcHandler(controller,factory,requestContext);  
  18. }  
  19. else  
  20. {  
  22. stringactionName=requestContext.RouteData.GetRequiredString("action");  
  23. returnIsAsyncAction(coreController,actionName,requestContext)?  
  24. (IHttpHandler)newAsyncMvcHandler(coreController,factory,requestContext):  
  25. (IHttpHandler)newSyncMvcHandler(controller,factory,requestContext);  
  26. }  
  27. }  
  29. internalstaticboolIsAsyncAction(  
  30. Controllercontroller,stringactionName,RequestContextrequestContext)  
  31. {  
  32. ...  
  33. }  
  1. Introduction to ASP. NET 2.0 Virtual Hosts
  2. Introduction to ASP. NET Applications
  3. Optimize ASP. NET 2.0 Profile Provider
  4. ASP. NET pipeline Optimization
  5. Introduction to ASP. NET Routing Engine

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.