December 2017 : Online courses in ASP.NET MVC and Angular 4. Conducted by Bipin Joshi. Read more...
Registration for December 2017 batches of ASP.NET MVC / Core and Angular 4 online courses have already started. Conducted by Bipin Joshi. Book your seat today ! Click here for more details.

URL Rewriting with ASP.NET

Introduction

In one of my recent applications I encountered a situations like this:

The client was having his web site hosted with one of the web hosts. He was also having multiple sub domain pointers i.e. the main domain was something like somedomain.com and there were sub domains like products.somedomain.com and support.somedomain.com pointing to the same location. Now he wanted that whenever somebody requests any page like support.somedomain.com/helpdesk.aspx then the page from somedomain.com/support/helpdesk.aspx be served. In other words he wanted to "hide" the actual URL http://www.somedomain.com/support/helpdesk.aspx from the end user. The end user instead always sees http://support.somedomain.com/helpdesk.aspx in the browser. This process of changing the URL from what was originally requested is called as URL rewriting. In this article we will develop one possible solution to the said problem.

What options are available for URL rewriting?

URL rewriting can be achieved with many ways. Following are more popular:

  • Use Application_BeginRequest event and HttpContext class to rewrite the URL. This is the simplest and quickest approach.
  • Develop an ASP.NET http module that serves the same purpose.
  • Develop an ISAPI filter that intercepts the requests and does the rewriting job.

In this article we will examine the first method. The reason I used this method in above described scenario is because of its simplicity and quick implementation. In advanced and more generic scenarios you may opt for the other two options.

The Application_BeginRequest Event

The Application_BeginRequest event is the first event to be fired in the HTTP pipeline chain of processing. This event is the right place to place URL rewriting code.

HttpContext Class

The HttpContext class contains HTTP-specific information about a specific HTTP request. This class also gives you access to other ASP.NET objects such as Request and Response. The class has a static property called Current that holds reference to the current application. The RewritePath() method of this class is the key to developing our URL rewriter. The method has two signatures in .NET framework 1.1:

public void RewritePath(string);
public void RewritePath(string, string, string);

The main role of RewritePath() method is to update the Request object's Path and QueryString properties as per our requirements. In other words it allows the requested URL to be different than the internal path of the page.

Example - Step-By-Step

  • Create a new ASP.NET Web Application project in C#
  • Open the web.config file and add following markup to it.
<appSettings>
<add key="productsSite" value="products"></add>
<add key="servicesSite" value="services"></add>
<add key="supportSite" value="support"></add>
</appSettings>   

We will store the physical folder names that contains the actual pages in <appSettings> section. We will read these settings in our code latter.

  • Create three folders in the application - Products, Services and Support.
  • Add one webform in each of the folders called MainPage.aspx
  • Open Global.asax and have a look at following event handler
protected void Application_BeginRequest
(Object sender, EventArgs e)
  • Add following code to the event handler:
string host,originalurl,newurl;
host=Request.Url.Host;
originalurl=Request.Url.PathAndQuery;

switch(host)
{
case "products.somedomain.com":
newurl="~/" + 
ConfigurationSettings.AppSettings["productsSite"] 
+ originalurl;
break;
case "services.somedomain.com":
newurl="~/" + 
ConfigurationSettings.AppSettings["servicesSite"] 
+ originalurl;
break;
case "support.somedomain.com":
newurl="~/" + 
ConfigurationSettings.AppSettings["supportSite"] 
+ originalurl;
break;
default:
newurl="~/" +
ConfigurationSettings.AppSettings["supportSite"] 
+ originalurl;
break;
}
HttpContext.Current.RewritePath(newurl);

Let's examine the code in bit details.

We first obtained the host address of the requested URL via Request.Url.Host property. In this case we will get it something like support.somedomain.com or somedomain.com.

We also obtained the path and query string of the requested page via Request.PathAndQuery property. In our scenario we will get it something like /helpdesk.aspx

The switch statement then tests the host and depending on the host we read the physical folder from the configuration file. We form the new URL from root to this folder (as indicated by ~ character).

Finally, we called RewritePath() method of HttpContext class and pass this new URL to it. As a result even though user requested support.somedomain.com we serve the page that actually resides in somedomain.com/support folder.

Summary

In this article we learnt to write URL rewriting code using Application_BeginRequest and HttpContext.RewritePath() method. This is a quick and easy method of supplying contents from URLs different than what were requested.

 

 


Bipin Joshi is a software consultant, an author and a yoga mentor having 22+ years of experience in software development. He also conducts online courses in ASP.NET MVC / Core and Design Patterns. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced the Yoga way of life he also teaches Meditation and Mindfulness to interested individuals. To know more about him click here.

Get connected : Twitter  Facebook  Google+  LinkedIn

Posted On : 05 September 2004


Tags : ASP.NET Web Forms Configuration