ASP. NET MVC Module

Source: Internet
Author: User

ASP. NET MVC Module

Objective

How is the pipeline model implemented in ASP. The relationship between HTTP module and HttpApplication is roughly described in the request processing process, and does not involve how the HTTP module is implemented and registered. The Http module is very significant, how does the ASP. NET MVC framework capture the request? Or in another way, where is the entry point for the ASP.

Http Module Registration using

We go directly to the topic to see the custom implementation, first we need to define a module before the module is used, the definition is simple, define a type and implement the IHttpModule interface example code 1-1

Code 1-1

1      Public classCustommodule:ihttpmodule2     {3          Public voidDispose ()4         {5             Throw Newnotimplementedexception ();6         }7 8          Public voidInit (HttpApplication context)9         {TenContext. BeginRequest + =NewEventHandler (context_beginrequest); OneContext. EndRequest + =NewEventHandler (context_endrequest); A         } -  -         voidContext_endrequest (Objectsender, EventArgs e) the         { -((HttpApplication) sender). Context.Response.Write (""); -         } -  +         voidContext_beginrequest (Objectsender, EventArgs e) -         { +((HttpApplication) sender). Context.Response.Write (""); A         } at}

This time our custom module has been defined, in the CustomModule type of Init () method is subscribed to the HttpApplication event, in the request processing process of the text said, this is not explained here.

It is not possible to run the program at this time, ASP. NET has a huge ability and do not know that you have customized a module of Ah, you have to tell it I have a custom module, run the time to execute my module.

Now let's register the module with the system, open the Web. config profile in the project, and find the <system.web> node add

Code 1-2

 <  system.web     >  <  httpmodules       >  <  add  name  = "Mycustommodule"   type  = "Mvcapplication.custommodule,mvcapplication"     />  </ httpmodules  >  </ system.web  >  

What is to say here is that the Name property "Mycustommodule" value is simply a module that is registered to the system by the HttpApplication instance object. modules["Mycustommodule"] By name gets the module that is already registered to the system

The value of the Type property is the namespace of the custom CustomModule type that is already the type name, and the value after the comma is the assembly name where the CustomModule type resides.

It is still not possible to run the program at this time because the blogger's sample program is a project using the MVC framework, so add an empty controller and a view.

    1. Right-click the Controllers folder add controller, set the name to HomeController, set the controller template to be an empty controller, and after clicking OK, you will see a default index () method in the controller.
    2. Right-click the index () method name and select Add view to pop up the dialog box and click Add.
    3. Add

So let's look at the results of the project:

Figure 1

UrlRoutingModule

The UrlRoutingModule type here is a system-defined module, why should it be explained? Because you can see the extension point of the system from inside it, it is also the connection point of the routing system and the MVC framework.

According to the previous space for routing learning, can be said to have a basic understanding of routing, we all know that the requested URL will go to match the system's defined route pattern, and then get a routedata such an object instance, And then according to the information inside it to the controller generation operations, and so on some subsequent behavior.

We look at the figure to understand the role of UrlRoutingModule, perhaps in the actual project development will not expand this part of the content, but it is no harm to know more about it.

Figure 2

The internal approximate implementation of the UrlRoutingModule type is as code 1-3

Code 1-3

1         voidContext_postresolverequestcache (Objectsender, EventArgs e)2         {3HttpApplication context = Sender asHttpApplication;4Httpcontextwrapper Contextwrapper =NewHttpcontextwrapper (context. Context);5 6Routedata Routedata =RouteTable.Routes.GetRouteData (contextwrapper);7 8RequestContext RequestContext =NewRequestContext (Contextwrapper, routedata);9IHttpHandler HttpHandler =RouteData.RouteHandler.GetHttpHandler (requestcontext);Ten httphandler.processrequest (context. Context); One}
Iroutehandler, IHttpHandler

In the above code 1-3, the HttpHandler variable is obtained from the request context object RequestContext by the Gethttphandler () method in the Routehandler property under the Routedata object.

Incidentally, about the RequestContext type (not the object type, the parameter context type) is often seen in the follow-up study of MVC, which is a pattern of development, HttpContextBase objects and Routedata objects are encapsulated in the RequestContext object.

Then the topic, Mvcroutehandler type has implemented the Iroutehandler type, careful friends in reading the previous article will find that in MVC when registering routes for each custom routing rule (that is, the route object) It is instantiated with the default Mvcroutehandler type, which translates into the Routedata routehandler attribute, in the default implementation of the Mvcroutehandler type, Gethttphandler () The method returns the Mvchandler, and then it is as stated above. A ProcessRequest () method is defined in the IHttpHandler interface, which is a process of the request-and-routing controller in the MVC framework. This section is described in the Controller section.

Jinyuan

Source: http://blog.csdn.net/jinyuan0829

This article is copyrighted by the author and Csdn, welcome reprint, but without the consent of the author must retain this statement, and on the article page

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.