ASP. net mvc Module

Source: Internet
Author: User

ASP. net mvc Module

 

Preface

How is the pipeline model implemented in ASP. NET? The article in the request processing process roughly describes the relationship between the Http Module and HttpApplication, but does not involve how to implement and register the Http Module. Http Module is of great significance. How does the ASP. net mvc framework capture requests? Or in another way, where is the entry point of the ASP. net mvc framework?

Http Module Registration

Let's go directly to the topic to see the custom implementation. First, before using the Module, we need to define a Module. The definition is very simple. We need to define a type and implement the IHttpModule interface sample code 1-1.

Code 1-1

1 public class CustomModule: IHttpModule 2 {3 public void Dispose () 4 {5 throw new NotImplementedException (); 6} 7 8 public void Init (HttpApplication context) 9 {10 context. beginRequest + = new EventHandler (context_BeginRequest); 11 context. endRequest + = new EventHandler (context_EndRequest); 12} 13 14 void context_EndRequest (object sender, EventArgs e) 15 {16 (HttpApplication) sender ). context. response. write ("

At this time, our custom Module has been defined. In the Init mmodule Init () method, we subscribe to the HttpApplication event, as described in the request processing process, I will not explain it here.

At this time, it is not feasible to run the program, ASP. NET has a huge capacity and does not know that you have customized a Module. You have to tell it that I have a custom Module, run this Module.

Now, register the Module to the system, open the Web. config configuration file in the project, and find the <system. web> node to add the

Code 1-2

<system.web>    

The value of "MyCustomModule" is the Name registered to the system by the Module. You can use the HttpApplication instance object. modules ["MyCustomModule"] obtains the Module that has been registered in the system according to the Name.

The Type attribute value is the Type name of the namespace of the custom mmmodule Type, and the value after the comma is the name of the assembly of the CustomModule Type.

 

At this time, the program still cannot be run, because the sample program of the blogger is a project using the MVC Framework, so you need to add an empty controller and a view.

 

Now let's take a look at the running results of the project:

Figure 1

UrlRoutingModule

The UrlRoutingModule type is a system-defined Module. Why should we explain it? Because it can be seen from the System Extension point, it is also because it is the connection point between the routing system and the MVC framework.

According to the previous article on routing, we can say that we have a basic understanding of routing. We all know that the requested Url will match the system-defined routing mode, then obtain an object instance such as RouteData, and then generate the controller based on the information in it.

Let's take a look at figure 1 to understand the role of UrlRoutingModule. It may not be able to expand this part of content in actual project development, but it is no harm to know more.

Figure 2

The internal implementation of the UrlRoutingModule type is roughly as follows: code 1-3

Code 1-3

 1         void context_PostResolveRequestCache(object sender, EventArgs e) 2         { 3             HttpApplication context = sender as HttpApplication; 4             HttpContextWrapper contextWrapper = new HttpContextWrapper(context.Context); 5  6             RouteData routeData = RouteTable.Routes.GetRouteData(contextWrapper); 7  8             RequestContext requestContext = new RequestContext(contextWrapper, routeData); 9             IHttpHandler httpHandler = routeData.RouteHandler.GetHttpHandler(requestContext);10             httpHandler.ProcessRequest(context.Context);11         }
IRouteHandler and IHttpHandler

In code 1-3 above, the httpHandler variable is obtained based on the request context object RequestContext through the GetHttpHandler () method in the RouteHandler attribute of the RouteData object.

Here refer to the following example: The RequestContext type (not the object type, but the parameter context type) is often seen in future MVC learning. This is also a development mode, the RequestContext object encapsulates the HttpContextBase object and RouteData object.

Next, the MvcRouteHandler type has already implemented the IRouteHandler type, when registering a Route in MVC, the default MvcRouteHandler type is used to instantiate each custom routing rule (that is, the Route object), which is changed to the RouteHandler attribute of RouteData, in the default implementation of the MvcRouteHandler type, the GetHttpHandler () method returns MvcHandler, and then as mentioned above. The IHttpHandler interface defines a ProcessRequest () method, which is a process of request-> route-> controller in the MVC framework. This part is described in the controller section.

 

 

Author: Jin Yuan

Source: http://www.cnblogs.com/jin-yuan/

The copyright of this article is shared by the author and the blog Park. You are welcome to reprint this article. However, you must keep this statement without the author's consent and go to the Article Page.

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.