ASP.NET 2.0 Unleashed
The simplest way to remap a URL is to specify the remapping in your application's web configuration file. For example, the web configuration file in Listing 19.1 remaps the Home.aspx page to the Default.aspx page. Listing 19.1. Web.Config
The configuration file in Listing 19.1 contains a <urlMappings> element. This element can contain one or more elements that remap a page from a URL to a mapped Url. CD Note The code samples in this section can be found in the UrlMappingsApp application on the CD that accompanies this book.
The mappedUrl attribute can contain query strings. However, it cannot contain wildcards. You can use the <urlMappings> element only when performing simple page-to-page mappings. After you add the web configuration file in Listing 19.1 to your application, any requests for the Home.aspx page are modified automatically to requests for the Default.aspx page. It doesn't matter whether the Home.aspx page actually exists. If the Home.aspx page does exist, you can never open the page. Note The tilde character (~) has a special meaning when used with a path. It represents the current application root. A forward slash (/) at the start of a URL, on the other hand, represents the website root. You can use the tilde only with properties of ASP.NET controls. For example, you can use it with the ASP.NET Image control's ImageUrl property, but you cannot use it with the HTML <img> src attribute. In code, you can use the tilde character with a path by using the Page.ResolveUrl() method. This method automatically expands the tilde to the application root.
When working with remapped URLs, you often need to determine the original URL that a user requested. For example, you might want to display a message that tells users to update their bookmarks (favorites) to point to the new URL. There are three properties you can use to determine the current URL:
The last property automatically replaces the name of the web application with a tilde (~) character. For example, the Default.aspx page in Listing 19.2 illustrates all three properties. Listing 19.2. Default.aspx
If you request the Home.aspx page, the request is remapped to the Default.aspx page by the web configuration file in Listing 19.1. The Page_Load() event handler displays a message asking users to update their bookmarks when the RawUrl does not match the path (see Figure 19.1). Figure 19.1. Remapping the Home page.
Each property displayed in the body of the page displays a different value: Request.RawUrl = /UrlMappingsApp/Home.aspx Request.Path = /UrlMappingsApp/Default.aspx Request.AppRelativeCurrentExecutionFilePath = ~/Default.aspx
Creating a Custom UrlRemapper Module
The <urlMappings> configuration element discussed in the previous section performs a very simple task. It remaps one page to another. However, you'll quickly discover that you need to perform more complex remappings. For example, imagine that you have a database that contains a table of product categories and a table of products. You want your website's users to request a URL that contains a product category and be able to see matching products. For example, if someone requests the /Products/Soda.aspx page, you want to display all the products in the Soda category. If someone requests the /Products/Milk.aspx page, you want to display all the products in the Milk category. In that case, you need to use a wildcard when matching URLs. When someone requests any path that matches the pattern /Products/*, you want to redirect the user to a page where you can display matching products for the category specified in the path. In this section, we create a custom HTTP module that remaps one URL to another. The module supports regular expression matching. Therefore it supports wildcard matches. CD Note The code samples in this section are located in the UrlRemapperApp application on the CD that accompanies this book.
The code for the custom modulenamed UrlRemapperis contained in Listing 19.3. Listing 19.3. UrlRemapper.vb
Notice that the class in Listing 19.3 implements the IHttpModule interface. An HTTP module is a special class that executes whenever you make a page request. HTTP Modules are discussed in detail in Chapter 25, "Working with the HTTP Runtime." The module in Listing 19.3 includes an Init() method. This method adds an event handler for the Application BeginRequest event. The BeginRequest event is the first event that is raised when you request a page. The BeginRequest handler gets a list of URL remappings from an XML file named UrlMappings.config. The contents of this XML file are cached in memory until the UrlMappings.config file is changed on the hard drive. Next, the module iterates through each remapping from the XML file and performs a regular expression match against the current URL. If the match is successful, then the Context.RewritePath() method is used to change the current path to the remapped path. Before you can use the module in Listing 19.3 in an application, you must first register the module in your application's web configuration file. The web configuration file in Listing 19.4 contains an <httpModules> element that includes the UrlRemapper module. Listing 19.4. Web.Config
A sample UrlMappings.config file is contained in Listing 19.5. Listing 19.5. UrlMappings.config
The XML file in Listing 19.5 contains two remappings. First, it remaps any request for the Home.aspx page to the Default.aspx page. Second, it remaps any request for any page in the Products directory to the Default.aspx page located in the Products folder. The second mapping uses a regular expression to match the incoming URL. The .* expression matches any sequence of characters. The Default.aspx page in the Products folder is contained in Listing 19.6. Listing 19.6. Products/Default.aspx
The Page_Load() event handler in Listing 19.6 grabs the path of the original request, using the Request.RawUrl property. Next, it extracts the filename from the path, using the System.IO.Path.GetFileNameWithoutExtension() method. Finally, it assigns the name of the page (the category name) to a Label and SqlDataSource control. Products that match the category are displayed in a GridView control. For example, if you request the /Products/Soda.aspx page, then all the products in the Soda category are displayed (see Figure 19.2). If you request the /Products/Milk.aspx page, then all products in the Milk category are displayed. Figure 19.2. Displaying matching products.
|
Категории