Let’s talk about the basic routing rule principles. The basic routing rules are from special In the general arrangement, the most special (non-mainstream) rules are at the front, and the most general (all-purpose) rules are at the end. This is because the matching routing rules are also in this order, so even if you write them in reverse order. If the routing rules are written correctly, you can still wait for 404.
XD Let’s talk about the structure of the URL first. In fact, this is not a structure, but a grammatical feature.
Personally, I think the first one is easier to understand, the second one is easier to debug, and the third one is more efficient to write. . Take what you want. This article prefers the third type.
##Routing rules
3. Customize regular variables. The variable URL segment (well, this translation reveals my IQ)
, in ViewAssignment cannot be compiled. If you access /Home/Index in this case, because the third segment (id) has no value, this parameter will be set to DefaultId## according to the routing rules. #This can be clearly seen by using viewbag to assign a value to the title
4. Let’s talk about the default route again
and then return to Default route. UrlParameter.Optional is called an optional URL segment. If there is no such parameter in the route, the id is
5. Variable length routing.
Here the id and the last segment are variable, so /Home/Index/dabdafdaf is equivalent to /Home/Index//abcdefdjldfiaeahfoeiho is equivalent to /Home/Index/All/Delete/Perm/. ....
6. Cross-namespace routing
In this way, if the first URL segment is not Home, it will be handed over to the second processing. Finally, you can also set this route to not be found if it is not found. Leave a way for the following routes, so you don’t have to look down anymore.
array will be ranked in no particular order. If there are multiple matching routes, an error will be reported. Then the author proposed an improved writing method. 7.
or official msdn
10.MVC5’s RouteAttribute
First, you must register the route in the route
This route feature is effective. This feature has several overloads. There are also routing constraints, order, route names, etc.
Others include routing prefixes and routing default values
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}$)} |
具体的可以参考
Attribute Routing in ASP.NET MVC 5
对我来说,这样的好处是分散了路由规则的定义.有人喜欢集中,我个人比较喜欢这种灵活的处理.因为这个action定义好后,我不需要跑到配置那里定义对应的路由规则
把这个节点里的preCondition删除,变成浏览网站,以开启 IIS Express,然后点显示所有应用程序-点击网站名称-配置(applicationhost.config)-搜索UrlRoutingModule节点
文件名还可以用 {filename}占位符。
IgnoreRoute方法是RouteCollection里面StopRoutingHandler类的一个实例。路由系统通过硬-编码识别这个Handler。如果这个规则匹配的话,后面的规则都无效了。 这也就是默认的路由里面routes.IgnoreRoute("{resource}.axd/{*pathInfo}");写最前面的原因。
最后还是再推荐一下Adam Freeman写的apress.pro.asp.net.mvc.4这本书。稍微熟悉MVC的从第二部分开始读好了。前面都是入门(对我来说是扯淡)。但总比国内某些写书的人好吧——把个开源项目的源代码下载下来帖到书上面来,然后标题起个深入解析XXXX,然后净瞎扯淡。最后一千多页的巨著又诞生了。Adam Freeman的风格我就很喜欢,都是实例写作,然后还在那边书里面专门写了大量的测试。
哎没办法啊,技术差距就是这样了。
The above is the detailed content of ASP.NET MVC routing configuration (detailed graphic and text explanation). For more information, please follow other related articles on the PHP Chinese website!