This walkthrough shows how to modify an existing ASP.NET Web site to
include ASP.NET routing features. At the end of this walkthrough the
site will have three new Web pages. Hyperlinks in one of the new pages
link to the other two pages by using routes. Because these hyperlinks
use routing to create the link URLs, they do not need to be changed if
the target pages change names or location.
The preceding procedure added an empty method that you can use to register routes. You will now add code to this method that adds routes to the Web site.
When you have finished this procedure, your application will accept URLs like the following:
In the RegisterRoutes method, add the following code:
In the RegisterRoutes method, add the following code:
In order to run this walkthrough, you must have the following:
This code passes the static (Shared in Visual Basic) Routes property of the RouteData class to RegisterRoutes.- Visual Studio 2010 or later versions.
- An
ASP.NET Web site that targets the .NET Framework version 4 or later
versions. If you do not already have a Web site to work with, see Walkthrough: Creating a Basic Web Page in Visual Studio.
Creating RoutesRoutes map URL patterns to physical files. To add routes to a Web site, you add them to the static (Shared in Visual Basic) Routes property of the RouteTable class by using the RouteCollection.MapPageRoute method.
In the following procedure, you create a method that you will use to add the routes. You call the new method from the Application_Start handler of the Global.asax page.
To add a method to the Global.asax file for adding routes
- If the Web site does not already have a Global.asax file, add one by following these steps:
- Right-click the Web project in Solution Explorer and then select Add New Item.
- Select Global Application Class and then click Add.
- Open the Global.asax file.
- After the Application directive, add an Import directive for the System.Web.Routing namespace, as shown in the following example:
<%@ Import Namespace="System.Web.Routing" %>
void RegisterRoutes(RouteCollection routes) { }
void Application_Start(object sender, EventArgs e) { RegisterRoutes(RouteTable.Routes); }
- If the Web site does not already have a Global.asax file, add one by following these steps:
The preceding procedure added an empty method that you can use to register routes. You will now add code to this method that adds routes to the Web site.
When you have finished this procedure, your application will accept URLs like the following:
- http://server/application/SalesReportSummary/2009
- http://server/application/SalesReport/en-US/2009
- http://server/application/ExpensesRoute/
- http://server/application/ExpensesRoute/2009/
- http://server/application/ExpensesRoute/en-US/2009?extrafield=value
To add routes
In the RegisterRoutes method, add the following code:routes.MapPageRoute("", "SalesReportSummary/{year}", "~/sales.aspx");
This code adds an unnamed route that has a URL pattern that contains the literal value "SalesReportSummary" and a placeholder (URL parameter) named year. It maps the route to a file that is named Sales.aspx.
In the RegisterRoutes method, add the following code:
routes.MapPageRoute("SalesRoute", "SalesReport/{locale}/{year}", "~/sales.aspx");This code adds a route that is named SalesRoute. You are naming this route because it has the same parameter list as the route that you will create in the following step. Assigning names to these two routes enables you to differentiate them when you generate URLs for them.
In the RegisterRoutes method, add the following code:
routes.MapPageRoute("ExpensesRoute", "ExpenseReport/{locale}/{year}/{*extrainfo}", "~/expenses.aspx", true, new RouteValueDictionary { { "locale", "US" }, { "year", DateTime.Now.Year.ToString() } }, new RouteValueDictionary { { "locale", "[a-z]{2}" }, { "year", @"\d{4}" } });
This code adds a route that is named ExpensesRoute. This route includes a catch-all parameter named extrainfo. The code sets the default value of the locale parameter to "US", and it sets the default value of the year parameter to the current year. Constraints specify that the locale parameter must consist of two alphabetic characters and the year parameter must consist of four numeric digits.
No comments:
Post a Comment