


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.
##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
. 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
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.
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!

The C#.NET ecosystem provides rich frameworks and libraries to help developers build applications efficiently. 1.ASP.NETCore is used to build high-performance web applications, 2.EntityFrameworkCore is used for database operations. By understanding the use and best practices of these tools, developers can improve the quality and performance of their applications.

How to deploy a C# .NET app to Azure or AWS? The answer is to use AzureAppService and AWSElasticBeanstalk. 1. On Azure, automate deployment using AzureAppService and AzurePipelines. 2. On AWS, use Amazon ElasticBeanstalk and AWSLambda to implement deployment and serverless compute.

The combination of C# and .NET provides developers with a powerful programming environment. 1) C# supports polymorphism and asynchronous programming, 2) .NET provides cross-platform capabilities and concurrent processing mechanisms, which makes them widely used in desktop, web and mobile application development.

.NETFramework is a software framework, and C# is a programming language. 1..NETFramework provides libraries and services, supporting desktop, web and mobile application development. 2.C# is designed for .NETFramework and supports modern programming functions. 3..NETFramework manages code execution through CLR, and the C# code is compiled into IL and runs by CLR. 4. Use .NETFramework to quickly develop applications, and C# provides advanced functions such as LINQ. 5. Common errors include type conversion and asynchronous programming deadlocks. VisualStudio tools are required for debugging.

C# is a modern, object-oriented programming language developed by Microsoft, and .NET is a development framework provided by Microsoft. C# combines the performance of C and the simplicity of Java, and is suitable for building various applications. The .NET framework supports multiple languages, provides garbage collection mechanisms, and simplifies memory management.

C# and .NET runtime work closely together to empower developers to efficient, powerful and cross-platform development capabilities. 1) C# is a type-safe and object-oriented programming language designed to integrate seamlessly with the .NET framework. 2) The .NET runtime manages the execution of C# code, provides garbage collection, type safety and other services, and ensures efficient and cross-platform operation.

To start C#.NET development, you need to: 1. Understand the basic knowledge of C# and the core concepts of the .NET framework; 2. Master the basic concepts of variables, data types, control structures, functions and classes; 3. Learn advanced features of C#, such as LINQ and asynchronous programming; 4. Be familiar with debugging techniques and performance optimization methods for common errors. With these steps, you can gradually penetrate the world of C#.NET and write efficient applications.

The relationship between C# and .NET is inseparable, but they are not the same thing. C# is a programming language, while .NET is a development platform. C# is used to write code, compile into .NET's intermediate language (IL), and executed by the .NET runtime (CLR).


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)