URL Remapping in ASP.NET

 

In Asp.Net remapping of url is very easy. To remap a URL is to specify the remapping in your application’s web configuration file. For example, below settings in the web configuration file remaps the Default.aspx page to the Default2.aspx page.

 

<configuration>

   <system.web>

                   <urlMappings>

                             <addurl="~/Default.aspx"mappedUrl="~/Default2.aspx"/>

                   </urlMappings>

  </system.web>

</configuration>

 

As shown above, the web.config file contains a <urlMappings> element. This element can contain one or more elements that remap a page from a URL to a mapped URL.

 

The content of Default.aspx

 

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml">

 

<head runat="server">

 

    <title>Default.aspx</title>

 

</head>

 

<body>

 

    <form id="form1" runat="server">

 

    <div>

 

      This is Default.aspx Page

 

    </div>

 

    </form>

 

</body>

 

</html>


 

and the content of Default2.aspx

 

<%@ Page Language="C#" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
 

<script runat="server">

</script>

 

<html xmlns="http://www.w3.org/1999/xhtml">

 

<head runat="server">

 

    <title>Default2.aspx</title>

 

</head>

 

<body>

 

    <form id="form1" runat="server">

 

    <div>

 

        This is Default2.aspx Page

 

    </div>

 

    </form>

 

</body>

 

</html>

 

So when you request for the Default.aspx, you will get the contents of Default2.aspx page as shown below.

 

 

 

As shown above, the url has Default.aspx page but page contains the Default2.aspx page content.

 

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 above changes to web configuration file to your application, any requests for the Default.aspx page are modified automatically to requests for the Default2.aspx page. It doesn’t matter whether the Default.aspx page actually exists.  Even if the Default.aspx page does exist, you can never open the page because your url remapping the Default.aspx page to Default2.aspx page.

 

                                                                                                         URLRemappingExp.zip (3.27 kb)