ASP. NET MVC Routing configuration (detailed)

Source: Internet
Author: User
Let's start with the basic http://www.php.cn/code/8703.html "target=" _blank "> Routing rules principles. The basic routing rules are from special to general arrangement, that is, the most special (non-mainstream) rules at the top, the most general (balm) rules at the end. This is because the matching routing rules are also in this order. If the write is reversed, then even if your routing rules are right, then wait for 404.

XD first says the construction of the URL. In fact, this is not the construction, but the grammatical characteristics of it.




URL constructs


Named parameter specification + anonymous object


Construct the route and add


Direct method overloading + anonymous objects


Personally feel that the first kind of easier to understand, the second convenient debugging, the third kind of writing more efficient bar. Picking it. This article is biased towards the third type.

Routing rules


1. Default route (MVC comes with)


2. Static URL segment

No placeholder routing is ready to write dead.

For example, it is absolutely no problem to write and visit http://localhost:XXX/Shop/OldAction.js,response. Controller, action, area these three reserved words do not set static variables inside.


3. Custom regular variable URL segment (well this translates exposure IQ)

The picture is not affixed, the result is that the title is displayed as Defaultid. Note that to assign values to the controller, the view assignment cannot be compiled. In this case, if you access/home/index, because the third paragraph (ID) has no value, this parameter will be set to Defaultid according to the routing rule .


This can be clearly seen by assigning a value to the title in ViewBag.

4. Re-stating the default route

Then go back to the default route. Urlparameter.optional This is called an optional URL segment. The ID is null if the parameter is not in the route. Roughly speaking, this optional URL segment can be used to achieve a separation of concerns. Just setting the default value of the parameter directly in the route is not really good. According to my understanding, the actual parameters are sent by the user, we only do the definition of formal parameter names. However, if you want to assign a default value to a parameter, it is recommended that you write the syntax sugar into the action parameter. Like what:

5. Variable-length routing.

Both the ID and the last paragraph are mutable here, so/home/index/dabdafdaf is equivalent to/home/index//abcdefdjldfiaeahfoeiho equivalent to/home/index/all/delete/perm/ .....


6. Cross-namespace routing

This reminds you to remember to refer to the namespace, open the IIS Web site, or 404. This is very non-mainstream, it is not recommended to engage in blind.

so the first URL segment is not home to the second processing finally can also set the route cannot find the words will not give back the route left behind, it will no longer look down. But in this case the array is ranked in no particular order, if there are multiple matching routes will be error. Then the author proposes an improved writing method.

7. Regular expression matching route


Constrain multiple URLs


8. Specify the Request method


9. WebForm Support


The concrete can see

Creating WebForm Apps with asp.net4 new feature routing

or the official MSDN

10.MVC5 's Routeattribute

The first thing to do in the route registration method there

This route feature is valid. There are several overloads for this feature. There are routing constraints Ah, order Ah, route name and so on.
The other is the route prefix, the route default value


Routing constructs


Parameter limits




Constraint Description Example
Alpha Matches uppercase or lowercase Latin alphabet characters (A-Z, A-Z) {X:alpha}
bool Matches a Boolean value. {X:bool}
Datetime Matches a DateTime value. {X:datetime}
Decimal Matches a decimal value. {X:decimal}
Double Matches a 64-bit floating-point value. {x:double}
Float Matches a 32-bit floating-point value. {X:float}
Guid Matches a GUID value. {X:guid}
Int Matches a 32-bit integer value. {X:int}
Length Matches a string with the specified length or within a specified range of lengths. {x:length (6)} {x:length (1,20)}
Long Matches a 64-bit integer value. {X:long}
Max Matches an integer with a maximum value. {X:max (10)}
MaxLength Matches a string with a maximum length. {x:maxlength (10)}
Min Matches an integer with a minimum value. {x:min (10)}
MinLength Matches a string with a minimum length. {x:minlength (10)}
Range Matches an integer within a range of values. {X:range (10,50)}
Regex Matches a regular expression. {X:regex (^\d{3}-\d{3}-\d{4}$)}

The specific can be referenced

Attribute Routing in ASP. NET MVC 5

The advantage for me is that it distracts from the definition of routing rules. Some people like to concentrate, I personally prefer this kind of flexible processing. Since this action is defined, I don't need to run to the configuration there to define the corresponding routing rules


11. Finally, I write a class to implement Irouteconstraint matching method.


12. Access to local documents For example, this is used to match whether the Web page is accessed using Google Chrome.

Delete the precondition in this node and turn it into a web browser to open IIS Express, Then click Show All Apps-click Site name-config (applicationhost.config)- search urlroutingModule node

13. Direct access to local resources, bypassing the routing system

The file name can also be used with the {filename} placeholder.

The Ignoreroute method is an instance of the Stoproutinghandler class inside the routecollection. The routing system recognizes this handler by hard-coding. If this rule matches, the subsequent rules are invalid. This is also the default route inside the routes. Ignoreroute ("{resource}.axd/{*pathinfo}"); write the most previous reason.


Routing test (on the basis of the test project, to install MOQ)



Finally, I would recommend Adam Freeman to write apress.pro.asp.net.mvc.4 the book. A little familiarity with MVC begins with the second part of reading. The front is all about getting started (for me it's nonsense). But it's better than some people who write books in China--Download the source code of an open source project to the top of the book, then the title of a deep analysis of XXXX, and then the net nonsense light. The last 1000 multi-page masterpiece was born again. Adam Freeman Style I like, are examples of writing, and then there is a book in there specifically written a lot of tests.

Hey no way, the technology gap is like this.


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.