URL Mapping

With the increasing complexity of many sites, it's important to provide not only easy-to-use menu controls but also easy-to-remember URLs. For example, consider the ASP.NET community site at http://www.asp.net/, as shown in Figure 9.4, which has a number of different tabs.

Figure 9.4. The ASP.NET community site menu

graphics/09fig04.gif

Although there are a number of tabs, all navigation goes through the default.aspx page, passing in the tab number as part of the query string. The hard-coding of these query string parameters means that should tabs be added, deleted, or reordered, bookmarked pages will potentially break.

To solve this problem, mapping URLs is now easy with a new section in the application configuration file, specifying the target URL and the actual URL. The syntax of the configuration section is shown below:

 <urlMappings enabled="[truefalse]">   <add     url="  String  "     mappedUrl="  String  " /> </urlMappings> 

The properties are detailed in Table 9.4.

For example, we could have the following code:

 <urlMappings enabled="true">   <add url="~/Home.aspx"     mappedUrl="~/default.aspx?tab=0" />   <add url="~/Forums/default.aspx"     mappedUrl="~/default.aspx?tab=1" /> </urlMappings> 

With the configuration shown above, any requests for Forums/default.aspx will result in default.aspx being called with the tab parameter set to 1 , but Forums/default.aspx will still be displayed in the browser search bar.

Table 9.4. urlMappings Properties

Property

Description

enabled

Indicates whether or not the urlMappings service is enabled. The default is true .

url

Sets the displayed URL. This must be a relative URL starting with ~/ .

mappedUrl

Sets the actual URL. This must be a relative URL starting with ~/ .

It should be noted that although a directory name alone can be included for the url attribute, no mapping will take place. This is because this is an ASP.NET service, and without a specified ASP.NET page, the URL mapping is not executed. For example, consider the following mapping:

 <add url="~/Forums/"   mappedUrl="~/default.aspx?tab=1" /> 

Although this is legal, ASP.NET never sees this request because it is for a directory; IIS sees there is no directory present and so issues a 404 error. Creating the directory doesn't work either because you'll then receive a Directory Listing Denied error or a directory listing if browsing is allowed. You can, however, create directories and empty ASP.NET pages purely to facilitate the mapping.



A First Look at ASP. NET v. 2.0 2003
A First Look at ASP. NET v. 2.0 2003
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 90

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net