This article is a mirror article of machine translation, please click here to jump to the original article.

View: 16017|Reply: 1

[.NET Core] ASP.NET Implementation of custom routing constraints in Core

[Copy link]
Posted on 5/6/2019 9:30:30 AM | | |
Routing constraints

ASP.NET Core, you can pass variables on URLs by defining routing templates, and you can provide defaults, optionals, and constraints for variables.

The constraint is used by adding the specified constraint name to the attribute route, which is used as follows:

Some constraints are already provided inside the framework, as follows:

Constraints
example
Examples of matches
illustrate
int
{id:int}
123456789, -123456789
Match any integer
bool
{active:bool}
true, FALSE
Match true or false (case sensitive)
datetime
{dob:datetime}
2016-12-31, 2016-12-31 7:32pm
Matches valid DateTime values (in fixed locality - see warning)
decimal
{price:decimal}
49.99, -1,000.01
Matches valid decimal values (in fixed locality - see warning)
double
{weight:double}
1.234, -1,001.01e8
Matches valid double values (in fixed locality - see warning)
float
{weight:float}
1.234, -1,001.01e8
Matches valid float values (in fixed locality - see warnings)
guid
{id:guid}
CD2C1638-1638-72D5-1638-DEADBEEF1638, {CD2C1638-1638-72D5-1638-DEADBEEF1638}
Matches valid GUID values
long
{ticks:long}
123456789, -123456789
Matches valid long values
minlength(value)
{username:minlength(4)}
Rick
The string must be at least 4 characters
maxlength(value)
{filename:maxlength(8)}
Richard
Strings must not exceed 8 characters
length(length)
{filename:length(12)}
somefile.txt
The string must be exactly 12 characters
length(min,max)
{filename:length(8,16)}
somefile.txt
The string must be at least 8 characters and no more than 16 characters
min(value)
{age:min(18)}
19
The integer value must be at least 18
max(value)
{age:max(120)}
91
The integer value must not exceed 120
range(min,max)
{age:range(18,120)}
91
The integer value must be at least 18 and not exceed 120
alpha
{name:alpha}
Rick
The string must consist of one or more alphabetic characters (a-z, case-sensitive).
regex(expression)
{ssn:regex(^\d{{3}}-\d{{2}}-\d{{4}}$)}
123-45-6789
The string must match the regular expression (see tips on defining regular expressions)
required
{name:required}
Rick
Used to enforce the presence of non-parametric values during URL generation


The built-in constraints work for most common use cases, but sometimes we still need to customize the effect we want.
Custom routing constraints

A custom constraint is to implement the IRouteConstraint interface and then overload the Match method, which has four parameters.

The first parameter, httpContext, is the context of the current request

The second parameter, route, is the route to which the current constraint belongs

The third parameter, routeKey, is the name of the variable currently checked, such as id in the example at the beginning of the article
The fourth parameter values is the dictionary value that the current URL matches, such as the route of the example at the beginning of the article, if the URL is users/1, then there is a dictionary with key = id , value = 1. Of course, there are also values for other variables, such as controller, action, etc.

The fifth parameter, routeDirection, is an enumerated value that represents whether the URL is requested by the web or generated by a method such as Url.Action.

For example, we want to define a constraint that specifies that the parameters passed by the route must be the specified enumeration value.

Let's define an enum first:


Then define the constraints:

Add custom constraints to the ConfigureServices method in Startup.cs:
Using constraints on routes:

(WebApplicationTest is the current namespace)


{id:int:min(2)} route must use min(2), otherwise there will be a conflict for id = 0 or id = 1.

Run the program and match our custom constraints when the routes are api/test/0, api/test/1, api/test/true, and api/test/false.

If the route is api/test/{integer greater than 2}, match the second route.

Other cases match the third route.

conclusion

Routing constraints are a very useful feature in some scenarios, which can reduce the check parameters in the controller, and the function of partial parameter validation can be implemented using declarative attruibute, and some duplicate checks can be extracted into constraints for public use.

The constructor of constraint can be injected, so it can be very extensible, and some parameter verification can be done by querying the database.

The official website only briefly mentions routing constraints, and this article provides specific examples of the use of routing constraints.





Previous:Zero Foundation C# (Full Color Version) Tomorrow Technology
Next:ASP.NET Core – Razor View Import Usage Description
 Landlord| Posted on 5/6/2019 9:32:15 AM |
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com