Home  >  Article  >  Backend Development  >  ASP.NET MVC routing configuration (detailed graphic and text explanation)

ASP.NET MVC routing configuration (detailed graphic and text explanation)

怪我咯
怪我咯Original
2017-03-30 11:48:371803browse

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.

ASP.NET MVC routing configuration (detailed graphic and text explanation)

##URL construction

Named parameter specification + anonymous
Object

Construct the route and then add

Direct
Method overloading+Anonymous object

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

## 1. Default routing (

MVC

comes with it).
2. Static URL segment

If there is no placeholder route, it is ready-made.

For example, write it like this and then access http://localhost: XXX/Shop/OldAction.js, response is completely fine. Don’t set the three reserved words controller, action, and area in static variables.




3. Customize regular variables. The variable URL segment (well, this translation reveals my IQ)


The picture is not posted, the result is that the title is displayed as DefaultId. Note that the value must be assigned in the

controller

, 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

null

. According to the original text, this optional URL segment can be used to achieve a separation of concerns. It is actually not very good to directly set the default value of the parameter in the route. According to my understanding, the actual parameters are sent by the user, and all we do is define the formal parameter name. However, if you insist on assigning default values ​​to the parameters, it is recommended. Syntactic sugar is written into the action parameter. For example:

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

This is a reminder to remember to quote the namespace and open the IIS website otherwise it will be 404. This is very non-mainstream and is not recommended to be messed with.




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.

But if it is written like this,

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.

Regular expression

Match route Constraints Multiple URLs

8. Specify the request method

9. WebForm supports

For details, please see

Usage Asp.Net4 new feature routing creates WebForm application


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定义好后,我不需要跑到配置那里定义对应的路由规则


11.最后还是不爽的话自己写个类实现 IRouteConstraint的匹配方法。


12.访问本地文档
比如这个就用来匹配是否是用谷歌浏览器访问网页的。

把这个节点里的preCondition删除,变成浏览网站,以开启 IIS Express,然后点显示所有应用程序-点击网站名称-配置(applicationhost.config)-搜索UrlRoutingModule节点

13.直接访问本地资源,绕过了路由系统

文件名还可以用 {filename}占位符。

IgnoreRoute方法是RouteCollection里面StopRoutingHandler类的一个实例。路由系统通过硬-编码识别这个Handler。如果这个规则匹配的话,后面的规则都无效了。 这也就是默认的路由里面routes.IgnoreRoute("{resource}.axd/{*pathInfo}");写最前面的原因。


路由测试(在测试项目的基础上,要装moq)



  最后还是再推荐一下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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn