Deep understanding of ASP. NET MVC (2)

Source: Internet
Author: User

Series Catalogue

how requests are entered into the MVC Framework (inbound)

When a URL request arrives, the system calls a registered ihttpmodules:urlroutingmodule, which will do the following :

First, in Routetable.route, start with the first route to find a route that matches the current URL. The following conditions need to be met in order to calculate the match:

1.URL matches the model of the URL in the route attribute;

2. all parameters defined in {} can be found in the URL of the request, or the parameter is defined in the defaults of the route, of course, the priority in defaults is lower than the definition in the URL, or the parameter is set to Urlparameter.optional;

3. The parameter satisfies the match rule defined by the route's Constraints , which may be a regular or a irouteconstraint object.

Second, the specified matching routebase will provide a routedata structure through the Getroutedata method ,Routedata contains the following four properties:

1.Route:route objects themselves;

2.RouteHandler: An object that implements the Iroutehandler, MVC provides a Mvcroutehandler class, from the Maproute source can be seen, The role is always assumed by Mvcroutehandler. Mvcroutehandler knows how to find the controller and action from the Routedata. In fact Mvcroutehandler will look for a key value of "controller" and "action" parameters, so we always need to include "controller" and "action" two parameters;

3.values: Holds the value dictionary of the parameters in the URL, the parameters are defined by {}, Route.defaults, and obtained from QueryString;

4.Datatokens: An additional dictionary, mainly used for the areas mechanism, will be discussed in detail later.

Note: If getroutedata returns NULL, it will not be able to enter the MVC framework, and the common case is that there is a direct static physical file that matches the URL, usually some resource files, such as css,image files. The routecollection can be set by setting the routeexistingfiles to true, making it useful for mismatched static files (note that true is mismatched, this is named and strange). The following code is the code snippet in Getroutedata , which shows how this mechanism is implemented.

12345678 if (!this.RouteExistingFiles){    string appRelativeCurrentExecutionFilePath = httpContext.Request.AppRelativeCurrentExecutionFilePath;    if (((appRelativeCurrentExecutionFilePath != "~/") && (this._vpp != null)) && (this._vpp.FileExists(appRelativeCurrentExecutionFilePath) || this._vpp.DirectoryExists(appRelativeCurrentExecutionFilePath)))    {        return null;    }}

Third, call Routehandler in Routedata, and provide Routehandler with context variables including routedata and httpcontextbase objects. and encapsulated into a parameter called RequestContext passed to the Routehandler. If you use Mvcroutehandler, you are now in the MVC framework .

Some points to note:

1. because of the urlroutingmodule search behavior mentioned above, the order in which the route is added in RouteCollection is important. Remember: give priority to adding the form special route;

2.Mvcroutehandler automatically provides parameters for the action when invoking the Controller's action. Parameters are derived from Routedata key-value pairs, Mvcroutehandler will automatically compare the key name and action parameter name, this comparison is case-sensitive, so in the action to write the parameter name should pay attention to, about this will be detailed after the expansion;

3. If you want the parameters in the URL to be optional, set the parameter to Urlparameter.optional.

custom parameter matching rules

The constraints of the route defines the parameter-matching rule in the URL, which may be a regular or a Irouteconstraint object . The familiar scenario is that the constraints is set to a regular by Maproute's simplified API settings. But what if our matching rules are more complicated? In fact, we can implement a Irouteconstraint object ourselves, Irouteconstraint has only one match method, so it's not difficult. One of my previous articles on ASP. NET MVC has a irouteconstraint implementation in the globalization scenario.

Another example is that the MVC framework has a httpmethodconstraint implementation of Irouteconstraint, which we can use as follows:

1234 routes. MapRoute ( null "Articles/{id}" ,   new {controller = "articles" "Show"   new {httpmethod = new httpmethodconstraint (   );

This means that only URLs that are requested by get can match the route. It is important to note that we often add [httpget] or [HttpPost] to the action to do a similar qualification, but the two are completely different, where the qualification is outside the MVC framework, and [HttpGet] is within the framework, obviously this method is more efficient.

Fruits of labor, reproduced please specify the source: http://www.cnblogs.com/P_Chou/archive/2010/11/02/details-asp-net-mvc-02.html

Deep understanding of ASP. NET MVC (2)

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.