ASP. net mvc: from HTTP request to controller-controller, how can I be precisely located?

Source: Internet
Author: User

... Preface

In Asp.net MVC, developers have to spend a lot of time designing the controller. When a user initiates an HTTP request, the Controller first gets a response.

Imagine a user sending a request http: // hostname: 80/azureapps/bookstore

Ask yourself the following two questions:

1) How does MVC locate zureappscontroller?

2). Uri does indicate that the controller is azureapps, but when naming the Controller class, why should we name it azureappscontroller?

... Prerequisites

[1]. Download ReflectorHttp://www.red-gate.com/products/reflector/And reflect classUrlroutingmodule: Ihttpmodule.

Figure 1: reflect urlroutingmodule

[2]. Download MVC source code http://www.microsoft.com/downloads/details.aspx? Familyid = 53289097-73ce-43bf-b6a6-35e00103cb4b & displaylang = EN,

And open the mvchandler class:

Figure 2: mvchandler

...... Q1: How does MVC locate zureappscontroller?

Figure 2 clearly tells us, oh, the Controller was originally found by mvchandler through the controllername passed by Uri.

But we didn't configure mvchandler in Web. config?

MVC uses mvcroutehandler to include mvchanlde in the routingmodule.

Code
Public class mvcroutehandler: iroutehandler {
Protected virtual ihttphandler gethttphandler (requestcontext ){
Return new mvchandler (requestcontext );
}

Iroutehandler members # region iroutehandler members
Ihttphandler iroutehandler. gethttphandler (requestcontext ){
Return gethttphandler (requestcontext );
}
# Endregion
}

 

But we have not configured "mvcroutehandler?

Haha, When you configure the route and call the extension method maproute, MVC has done something.

 

Code
[Suppressmessage ("Microsoft. Design", "ca1054: uriparametersshouldnotbestrings", messageid = "2 #",
Justification = "This is not a regular URL as it may contain special routing characters.")]
Public static route maproute (this routecollection routes, string name, string URL, object defaults, object constraints, string [] namespaces ){
If (routes = NULL ){
Throw new argumentnullexception ("Routes ");
}
If (url = NULL ){
Throw new argumentnullexception ("url ");
}

Route route = new route (URL, new mvcroutehandler ()){
Defaults = new routevaluedictionary (defaults ),
Constraints = new routevaluedictionary (constraints)
};

If (namespaces! = NULL) & (namespaces. length> 0 )){
Route. datatokens = new routevaluedictionary ();
Route. datatokens ["namespaces"] = namespaces;
}

Routes. Add (name, route );

Return route;
}

 

Now, open reflector and you can see clearlyUrlroutingmoduleThe onapplicationpostmaprequesthandler is registered during the initialization module.

Onapplicationpostmaprequesthandler obtains mvchandler through the mvcroutehandler specified by maproute.

In processrequest, mvchandler finds the corresponding Controller Based on controllername and instantiates it.

... Summary

I hope this article is helpful to colleagues who are not very clear about the first question, just like me.

Next question:

Q2: Why must I name the Controller class zurepoliccontroller? 

Due to limited time, leave it for the next time.

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.