URL Routing was first introduced in the ASP.NET MVC framework and was later added to the ASP.NET Web Forms framework. In this lesson I show you how to enable and use routing in an ASP.NET Web Forms framework web application.
Only the MVC templates and the ASP.NET Dynamic Data templates have routing enabled by default. The ASP.NET Web Application template that I have been using in all of my examples does not have routing enabled by default. These are the four steps that must be completed to add routing to a web application that was created using a template that does not have routing enabled:
Add a reference to the System.Web.Routing assembly (see Figure 15A-1).
Figure 15A.1. FIGURE 15A-1
Add the following using
statement to the Global.asax.cs
file:
using System.Web.Routing;
Add the following RegisterRoutes
method to the Global.asax.cs
file:
void RegisterRoutes(RouteCollection routes) { )
Add the following line to the Application_Start
method in the Global.asax.cs
file:
RegisterRoutes(RouteTable.Routes);
Now you can start using routing in your ASP.NET Web Forms application.
ASP.NET Web Forms uses the MapPageRoute
method of the RouteCollection
class to define new routes. The MapPageRoute
method can include the following parameters:
routeName — This is a string representing the name of the route.
routeUrl — This is a string that contains the URL pattern for the ...
No credit card required