The preceding illustration of Url.Action assumes standard routing. URL era functions in the same way with attribute routing, while the ideas are distinctive. With typical routing:
Attribute routes can also be coupled with inheritance. That is potent coupled with token substitution. Token replacement also relates to route names described by attribute routes.
The ControllerBase and Controller base classes offer comfort methods for motion results that reference A different motion. Just one common utilization will be to redirect immediately after accepting user input:
Attribute routes assist the exact same inline syntax as traditional routes to specify optional parameters, default values, and constraints.
Common routing is order-dependent. Normally, routes with regions need to be put earlier as they're much more certain than routes with no a location.
We can even constrain the route applying its worth constraint. Like in previously mentioned illustration, previously mentioned route will likely be relevant to only All those request whose controller name starts with "H", motion identify is both Index or About, ask for style is GET and worth of id is between ten and 20.
From the preceding code, MapControllers known as inside UseEndpoints to map attribute routed controllers.
It is routing in asp.net mvc much better to utilize the greater precise HTTP verb attribute to generally be exact about what your API supports. Purchasers of REST APIs are envisioned to understand what paths and HTTP verbs map to precise sensible operations.
The site route within the previous code is a dedicated conventional route. It is really named a devoted conventional route because:
The previous code is undoubtedly an case in point or bad routing layout. It had been applied to illustrate the Order property.
In case you are looking through this within a language aside from English, allow us to know On this GitHub dialogue challenge if you'd like to begin to see the code reviews with your native language.
I by now confirmed that it is achievable to set default values for controller, actions and attributes within the route. Additionally it is attainable to set default values for characteristics from the motion. This is certainly performed as in usual C# with variable = defaultValue, such as string id = “one”.
You may as well utilize constraints on the value with the parameter by configuring route constraints. For instance, the next route applies a limitation to the id parameter which the id's benefit should be numeric.
Putting a number of route attributes to the controller implies that each brings together with Every single in the route attributes about the action strategies: