Route Attribute at the Controller Level
You will notice that, with the URL pattern for the action
methods, Index
and Index2
, we repeat the controller name, Home
, in both URL patterns, Home
and Home/Index3
. Instead of repeating the controller
method name (or any common part in the URL) at the action
method level, we can define it at the controller level.
In the following code, the common part of the URL (Home) is defined at the controller level and the unique part is defined at the action
method level. When the URL pattern is getting mapped to the action
methods of the controller, both route parts (at the controller
level and at the action
method level) are merged and matched. So, there will be no difference between the routes defined earlier and those that follow.
If you want any parameters in attribute-based routing, you can pass them within curly brackets. In the following example, we did this for the SayHello
action method.
For example, the http://localhost:49831/Home/Index3
URL pattern...