"
ASP.NET (snapshot 2017) Microsoft documentation and samples

ASP.NET MVC Routing Overview (C#)

by Stephen Walther

In this tutorial, Stephen Walther shows how the ASP.NET MVC framework maps browser requests to controller actions.

In this tutorial, you are introduced to an important feature of every ASP.NET MVC application called ASP.NET Routing. The ASP.NET Routing module is responsible for mapping incoming browser requests to particular MVC controller actions. By the end of this tutorial, you will understand how the standard route table maps requests to controller actions.

Using the Default Route Table

When you create a new ASP.NET MVC application, the application is already configured to use ASP.NET Routing. ASP.NET Routing is setup in two places.

First, ASP.NET Routing is enabled in your application’s Web configuration file (Web.config file). There are four sections in the configuration file that are relevant to routing: the system.web.httpModules section, the system.web.httpHandlers section, the system.webserver.modules section, and the system.webserver.handlers section. Be careful not to delete these sections because without these sections routing will no longer work.

Second, and more importantly, a route table is created in the application’s Global.asax file. The Global.asax file is a special file that contains event handlers for ASP.NET application lifecycle events. The route table is created during the Application Start event.

The file in Listing 1 contains the default Global.asax file for an ASP.NET MVC application.

Listing 1 - Global.asax.cs

[!code-csharpMain]

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Web;
   5:  using System.Web.Mvc;
   6:  using System.Web.Routing;
   7:   
   8:  namespace MvcApplication1
   9:  {
  10:      // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
  11:      // visit http://go.microsoft.com/?LinkId=9394801
  12:   
  13:      public class MvcApplication : System.Web.HttpApplication
  14:      {
  15:          public static void RegisterRoutes(RouteCollection routes)
  16:          {
  17:              routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  18:   
  19:              routes.MapRoute(
  20:                  "Default",                                              // Route name
  21:                  "{controller}/{action}/{id}",                           // URL with parameters
  22:                  new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
  23:              );
  24:          }
  25:   
  26:          protected void Application_Start()
  27:          {
  28:              RegisterRoutes(RouteTable.Routes);
  29:          }
  30:      }
  31:  }

When an MVC application first starts, the Application_Start() method is called. This method, in turn, calls the RegisterRoutes() method. The RegisterRoutes() method creates the route table.

The default route table contains a single route (named Default). 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.

Imagine that you enter the following URL into your web browser’s address bar:

/Home/Index/3

The Default route maps this URL to the following parameters:

When you request the URL /Home/Index/3, the following code is executed:

HomeController.Index(3)

The Default route includes defaults for all three parameters. If you don’t supply a controller, then the controller parameter defaults to the value Home. If you don’t supply an action, the action parameter defaults to the value Index. Finally, if you don’t supply an id, the id parameter defaults to an empty string.

Let’s look at a few examples of how the Default route maps URLs to controller actions. Imagine that you enter the following URL into your browser address bar:

/Home

Because of the Default route parameter defaults, entering this URL will cause the Index() method of the HomeController class in Listing 2 to be called.

Listing 2 - HomeController.cs

[!code-csharpMain]

   1:  using System.Web.Mvc;
   2:   
   3:  namespace MvcApplication1.Controllers
   4:  {
   5:      [HandleError]
   6:      public class HomeController : Controller
   7:      {
   8:          public ActionResult Index(string id)
   9:          {
  10:              return View();
  11:          }
  12:      }
  13:  }

In Listing 2, the HomeController class includes a method named Index() that accepts a single parameter named Id. The URL /Home causes the Index() method to be called with an empty string as the value of the Id parameter.

Because 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.

Listing 3 - HomeController.cs (Index action with no parameter)

[!code-csharpMain]

   1:  using System.Web.Mvc;
   2:   
   3:  namespace MvcApplication1.Controllers
   4:  {
   5:      [HandleError]
   6:      public class HomeController : Controller
   7:      {
   8:          public ActionResult Index()
   9:          {
  10:              return View();
  11:          }
  12:      }
  13:  }

The Index() method in Listing 3 does not accept any parameters. The URL /Home will cause this Index() method to be called. The URL /Home/Index/3 also invokes this method (the Id is ignored).

The URL /Home also matches the Index() method of the HomeController class in Listing 4.

Listing 4 - HomeController.cs (Index action with nullable parameter)

[!code-csharpMain]

   1:  using System.Web.Mvc;
   2:   
   3:  namespace MvcApplication1.Controllers
   4:  {
   5:      [HandleError]
   6:      public class HomeController : Controller
   7:      {
   8:          public ActionResult Index(int? id)
   9:          {
  10:              return View();
  11:          }
  12:      }
  13:  }

In Listing 4, the Index() method has one Integer parameter. Because the parameter is a nullable parameter (can have the value Null), the Index() can be called without raising an error.

Finally, invoking the Index() method in Listing 5 with the URL /Home causes an exception since the Id parameter is not a nullable parameter. If you attempt to invoke the Index() method then you get the error displayed in Figure 1.

Listing 5 - HomeController.cs (Index action with Id parameter)

[!code-csharpMain]

   1:  using System.Web.Mvc;
   2:   
   3:  namespace MvcApplication1.Controllers
   4:  {
   5:      [HandleError]
   6:      public class HomeController : Controller
   7:      {
   8:          public ActionResult Index(int id)
   9:          {
  10:              return View();
  11:          }
  12:      }
  13:  }

Invoking a controller action that expects a parameter value

Figure 01: Invoking a controller action that expects a parameter value (Click to view full-size image)

The URL /Home/Index/3, on the other hand, works just fine with the Index controller action in Listing 5. The request /Home/Index/3 causes the Index() method to be called with an Id parameter that has the value 3.

Summary

The goal of this tutorial was to provide you with a brief introduction to ASP.NET Routing. We examined the default route table that you get with a new ASP.NET MVC application. You learned how the default route maps URLs to controller actions.

Next



Comments ( )
Link to this page: //www.vb-net.com/AspNet-DocAndSamples-2017/aspnet/mvc/overview/older-versions-1/controllers-and-routing/asp-net-mvc-routing-overview-cs.htm
< THANKS ME>