ASP.NET Web Forms Extensibility: URL Mapping

A long time before ASP.NET Routing came along, ASP.NET already offered a similar functionality: it was called URL mapping.

URL mapping allows having virtual URLs that redirect to real ones. For example, you can have all requests for “/Product/IPhone” redirected to “/Product.aspx?ID=10”. This allows two simple things:

  • Hiding complexity (the ID parameter, for example);
  • Hiding the technology in use (in this case, the .ASPX extension is never seen);
  • Redirecting from one page (such as Default.aspx) to another transparently.

You can configure URL mapping by just adding entries to the urlMapping section on Web.config:

   1: <urlMappings enabled="true">
   2:     <add url="~/Product/IPhone" mappedUrl="~/Product.aspx?ID=10"/>
   3: </urlMappings>

And that’s it, no modules or additional configuration.

You can keep using the QueryString collection:

   1: Int32 id = Convert.ToInt32(this.Request.QueryString["ID"]);     //10
   2: String path = this.Request.Path;                                //Product.aspx

The Url property also contains the real URL, but the RawUrl contains the requested URL:

   1: String requestedPath = this.Request.RawUrl;    //Product/IPhone
   2: String mappedPath = this.Request.Url;          //http://localhost/Product.aspx?ID=1

So, as you can see, this is much simpler that ASP.NET Routing and should only be used in very simple scenarios: when you are using a version of ASP.NET prior to 3.5, or you want to configure routes only through the Web.config file.


No Comments