Everything about routing in asp.net mvc
Everything about routing in asp.net mvc
Blog Article
Attribute routes will also be coupled with inheritance. This is strong coupled with token alternative. Token alternative also applies to route names defined by attribute routes.
To accomplish this, we could configure the MapControllerRoute process, as revealed during the graphic down below. In this article, you can see We've got specified the pattern as Scholar/All as well as default controller and action identify as controller = Student, motion = Index.
Let's look at a handful of samples of how the Default route maps URLs to controller actions. Think about that you enter the following URL into your browser tackle bar:
The very first two controllers are associates of regions, and only match when their respective location title is furnished by the region route worth. The 3rd controller is not a member of any area, and will only match when no benefit for location is supplied by routing.
Our content lets you master technologies conveniently and rapidly for learners of all ranges. By accessing this platform, you admit that you've got reviewed and consented to abide by our Terms of Use and Privateness Plan, intended to safeguard your knowledge and privateness rights.
As demonstrated in the above mentioned code, the URL sample for the scholar route is "pupils/ id ", which specifies that any URL that begins with domainName/learners, needs to be taken care of by the StudentController. Recognize that we haven't specified " motion " in the URL sample because we wish every URL that starts off with learners should really normally make use of the Index() motion with the StudentController class.
So Although maybe you have the ValuesController derive from ApiController As well as in the same folder because the HomeController, it is going to continue to be mapped to hxxp:///api/Values. Here ‘api’ will be the static Section of a route. Upcoming We're going to see how we can easily add our personal custom made routes for MvcControllers.
As you can see in the above mentioned code, the Index action method usually takes no parameters, even though the Details motion technique takes a person parameter. We need to invoke the Index motion strategy with out a parameter as follows.
Equally, if we problem a ask for for the /Home/Information/two URL, then the small print motion way of the house Controller class will cope with the request, as demonstrated in the impression down below. Listed here, the parameter benefit 2 is mechanically mapped on the id parameter of the main points action system.
The Default route consists of defaults for all a few parameters. If you do not source a controller, then the controller parameter defaults to the value Home.
You may perhaps hope to hit this problem with the default route controller / motion / id? . This problem is rare in exercise due to the fact Url.Action normally explicitly specifies a controller and action value.
If you are reading through this in a very language aside from English, let us know Within this GitHub discussion problem if you would like to see the code reviews inside your native language.
When routing performs URL era, the values furnished need to match the default values. URL technology working with weblog fails as the values controller = House, motion = Index don't match controller = Blog site, action = Short article . Routing then falls back to test default, which succeeds.
The preceding illustrations confirmed using routing in asp.net mvc IUrlHelper in a very controller. The most typical utilization inside of a controller would be to make a URL as part of an motion outcome.