In-depth understanding of ASP. net mvc (2)

Source: Internet
Author: User

 

Series directory

How the request enters the MVC Framework (inbound)

When a URL request arrives, the system calls a registeredIhttpmodules:Urlroutingmodule,It will do the following:

1. In routetable. RouteSearch for the first routeMatch the route of the current URL. Matching can be calculated only when the following conditions are met:

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

2.All the parameters defined in {} can be found in the request URL, or the parameters are also defined in route defaults. Of course, the priority defined in defaults is lower than that defined in the URL, or set the parameterUrlparameter. Optional;

3.If the parameter is set to routeConstraintsThe rule may be a regular expression orIrouteconstraintObject.

2. The specified routebase will passGetroutedata MethodProvideRoutedataStructure,RoutedataIt includes the following four attributes:

1.Route: The route object itself;

2. routehandler: An object that implements iroutehandler. MVC providesMvcroutehandlerClass. From the source code of maproute, we can see that mvcroutehandler is always used as the role.Mvcroutehandler knows how to find controller and action from routedata. In fact, mvcroutehandler will look for "controller" and "action" parameters, so we always need to include "controller" and "action" parameters;

3.Values: Save the value Dictionary of parameters in the URL. The parameters include defined by {}, in route. defaults, and obtained from querystring;

4.Datatokens: an additional Dictionary, mainly used for the areas mechanism. It will be discussed in detail later.

NOTE: IfGetroutedataIf the return value is null, you will not be able to enter the MVC framework. A common situation is that a direct static physical file can match this URL, usually some resource files, such as CSS and image files. You can setRouteexistingfilesIsTrueForMismatchStatic files (note that true does not match. This name is strange ). The followingCodeYesGetroutedataThe code segment in shows how this mechanism is implemented.

If (! This. routeexistingfiles) {string apprelativecurrentexecutionfilepath = httpcontext. Request. apprelativecurrentexecutionfilepath; If (apprelativecurrentexecutionfilepath! = "~ /") & (This. _ VPP! = NULL) & (this. _ Vpp. fileexists (apprelativecurrentexecutionfilepath) | this. _ Vpp. directoryexists (apprelativecurrentexecutionfilepath) {return NULL ;}}

3. Call routehandler in routedata and provideRoutedataAndHttpcontextbaseObjects and other context variables, which are encapsulated into a parameter called requestcontext and passed to routehandler. If mvcroutehandler is used,So far, we have entered the MVC framework..

 

Notes:

1.Because of the search behavior of the urlroutingmodule mentioned above, the order in which route is added to routecollection is very important. Remember:Special route;

2.Mvcroutehandler automatically provides parameters for the action when calling the Controller action. The parameter is derived from the routedata key-value pair. mvcroutehandler automatically compares the key name with the parameter name of the action. This comparison is case sensitive. Therefore, you must note the parameter name when writing the parameter name in the action, this will be detailed later;

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

 

Custom parameter matching rules

Route constraints defines the parameter matching rules in the URL,The rule may be a regular expression or an irouteconstraint object.. The familiar situation is that constraints is set as a regular expression through the simplified API of maproute. But what if our matching rules are complicated? In fact, we can implement an irouteconstraint object by ourselves. irouteconstraint has only one match method, so it is not difficult. My previous articleArticleThe ASP. net mvc globalization solution has an irouteconstraint implementation.

Another example is the MVC framework.HttpmethodconstraintIrouteconstraint is implemented, which can be used as follows:

Routes. maproute (null, "articles/{ID}", new {controller = "articles", Action = "show "}, new {httpmethod = new httpmethodconstraint ("get ")});

This indicates that only the URL requested in the get method can match this route. Note that we often add [httpget] or [httppost] To the action to make similar restrictions, but the two are completely different. The limitations here are outside the MVC framework, while [httpget] is in the framework, it is obvious that this method is more efficient.

Labor fruit, reproduced please indicate the source: http://www.cnblogs.com/P_Chou/archive/2010/11/02/details-asp-net-mvc-02.html

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.