Web.Config
<configuration>
<system.web>
<urlMappings>
<add url=”~/Index.aspx” mappedUrl=”~/Default.aspx”/>
</urlMappings>
</system.web>
</configuration>
The configuration file ontains a <urlMappings> element. This element can contain one or more elements that remap a page from a URL to a mapped Url.
The mappedUrl attribute can contain query strings. However, it cannot contain wildcards.You can use the <urlMappings> element only when performing simple page-topage mappings.
After you add the web configuration file in 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.
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, 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.
You can use the following to determine the current URL:
- Request.RawUrl : Returns the original URL (before being remapped).
- Request.Path : Returns the current URL (after being remapped).
- Request.AppRelativeCurrentExecutionFilePath : Returns the application relative URL (after being remapped).
<%@ Page Language=”C#” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN”
“http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
<script runat=”server”>
void Page_Load()
{
if (String.Compare(Request.Path, Request.RawUrl, true) != 0)
lblMessage.Text = “The URL to this page has changed, “ +
“please update your bookmarks.”;
}
</script>
<html xmlns=”http://www.w3.org/1999/xhtml” >
<head runat=”server”>
<style type=”text/css”>
html
{
font:14px Georgia,Serif;
}
.message
{
border:Dotted 2px red;
background-color:yellow;
}
</style>
<title>Default Page</title>
</head>
<body>
<form id=”form1” runat=”server”>
<div>
<h1>The Default Page</h1>
<p>
<asp:Label id=”lblMessage” CssClass=”message” Runat=”server” />
</p>
The original request was for:
<blockquote>
<%=Request.RawUrl%>
</blockquote>
which got remapped to:
<blockquote>
<%= Request.Path %>
</blockquote>
and the application relative version is:
<blockquote>
<%= Request.AppRelativeCurrentExecutionFilePath %>
</blockquote>
</div>
</form>
</body>
</html>
If you request the Index.aspx page, the request is remapped to the Default.aspx page by the web configuration file . The Page_Load() event handler displays a message asking users to update their bookmarks when the RawUrl does not match the path.
Each property displayed in the body of the page displays a different value:
Request.RawUrl = /UrlMappingsApp/Index.aspx
Request.Path = /UrlMappingsApp/Default.aspx
Request.AppRelativeCurrentExecutionFilePath = ~/Default.aspx