
We examined the default route table that you get with a new ASP.NET MVC application. The goal of this tutorial was to provide you with a brief introduction to ASP.NET Routing. The request /Home/Index/3 causes the Index() method to be called with an Id parameter that has the value 3. The URL /Home/Index/3, on the other hand, works just fine with the Index controller action in Listing 5. Listing 5 - HomeController.cs (Index action with Id parameter) using įigure 01: Invoking a controller action that expects a parameter value ( Click to view full-size image) If you attempt to invoke the Index() method then you get the error displayed in Figure 1. Because the parameter is a nullable parameter (can have the value Null), the Index() can be called without raising an error.įinally, invoking the Index() method in Listing 5 with the URL /Home causes an exception since the Id parameter is not a nullable parameter. In Listing 4, the Index() method has one Integer parameter. Listing 4 - HomeController.cs (Index action with nullable parameter) using The URL /Home also matches the Index() method of the HomeController class in Listing 4. The URL /Home/Index/3 also invokes this method (the Id is ignored). The URL /Home will cause this Index() method to be called. The Index() method in Listing 3 does not accept any parameters. Listing 3 - HomeController.cs (Index action with no parameter) using The URL /Home causes the Index() method to be called with an empty string as the value of the Id parameter.īecause of the way that the MVC framework invokes controller actions, the URL /Home also matches the Index() method of the HomeController class in Listing 3.

In Listing 2, the HomeController class includes a method named Index() that accepts a single parameter named Id. Imagine that you enter the following URL into your browser address bar:īecause of the Default route parameter defaults, entering this URL will cause the Index() method of the HomeController class in Listing 2 to be called. Let's look at a few examples of how the Default route maps URLs to controller actions. Finally, if you don't supply an id, the id parameter defaults to an empty string. If you don't supply an action, the action parameter defaults to the value Index. If you don't supply a controller, then the controller parameter defaults to the value Home. The Default route includes defaults for all three parameters.
#Index enroute 4 code#
When you request the URL /Home/Index/3, the following code is executed: The Default route maps this URL to the following parameters: Imagine that you enter the following URL into your web browser's address bar:

The Default route maps the first segment of a URL to a controller name, the second segment of a URL to a controller action, and the third segment to a parameter named id. The default route table contains a single route (named Default). The RegisterRoutes() method creates the route table. This method, in turn, calls the RegisterRoutes() method. When an MVC application first starts, the Application_Start() method is called. Routes.IgnoreRoute(" // Parameter defaults Public static void RegisterRoutes(RouteCollection routes) Note: For instructions on enabling IIS6 or IIS7 classic mode, The file in Listing 1 contains the default Global.asax file for an ASP.NET MVC application. The route table is created during the Application Start event. The Global.asax file is a special file that contains event handlers for ASP.NET application lifecycle events.

Second, and more importantly, a route table is created in the application's Global.asax file. Be careful not to delete these sections because without these sections routing will no longer work. There are four sections in the configuration file that are relevant to routing: the section, the section, the section, and the section. ASP.NET Routing is setup in two places.įirst, ASP.NET Routing is enabled in your application's Web configuration file (Web.config file). When you create a new ASP.NET MVC application, the application is already configured to use ASP.NET Routing. By the end of this tutorial, you will understand how the standard route table maps requests to controller actions. The ASP.NET Routing module is responsible for mapping incoming browser requests to particular MVC controller actions. In this tutorial, you are introduced to an important feature of every ASP.NET MVC application called ASP.NET Routing. In this tutorial, Stephen Walther shows how the ASP.NET MVC framework maps browser requests to controller actions.
