Home   |   QuickStart Welcome   |   ASP.NET   |   Web Services   |   How Do I...?   
  |   I want my samples in...      

ASP.NET 2.0 Quickstart Tutorials

URL Mapping

The URL mapping feature uses configuration information stored in web.config to remap incoming requests to a different URL. The remapping occurs prior to any other processing for the inbound request. Although the sample below demonstrates remapping a page request, any arbitrary file type can have its request remapped to a different URL.

Defining Remapped URLs

Configuration information for URL Mapping is stored in web.config. Each <add> element nested within the <urlMappings > element defines a rule for remapping an inbound url. The url attribute defines the exact value of the inbound url that the URL Mapping feature will attempt to match. If an exact match occurs, the inbound URL is rewritten to the value of the mappedUrl attribute. Note that the feature does not support more advanced rules such as wildcard or regular expression based matches.

The sample web.config defines mapping rules for a number of urls. The web.sitemap file for the sample defines most of its nodes with URL values that that will be remapped. As a result, the combination of URL Mapping and Site Navigation allows a developer to define a navigation structure in terms of friendly urls, while using URL Mapping to rewrite requests to a different set of pages that perform the actual processing.

When you run the sample, notice how the Menu and Treeview controls render navigation data based on the site structure defined in the web.sitemap file. If you hover over any of the links in the Treeview control or the SiteMapPath control in the upper righthand corner, the url that is shown in the status bar is a friendly url. When you click on any of the navigation links, the actual page that runs is Default.aspx. However, the navigation information in the Menu, Treeview and SiteMapPath controls continues to reflect the friendly URL structure.

At the bottom of the page you can also see the values returned from Request.Path, Request.QueryString["category"], and Request.RawUrl . The values for Request.Path and Request.QueryString["category"] always reflect the results of remapping the inbound url. However, the value for Request.RawUrl reflects the friendly url prior to remapping. The Site Navigation feature always uses the Request.RawUrl when attempting to match url information to data contained in a sitemap file. If a matching value is not found, then the XmlSiteMapProvider falls back and uses the Request.Path instead. In this sample, all of the friendly urls have an entry in the web.sitemap file, so the controls that use Site Navigation always display and reference nodes based on the friendly url.

VB Url Mapping
Run Sample View Source