Tuesday, January 10, 2006

Simple Redirect

Overview

Simple Redirect performs a redirect from one IIS Virtual Directory to another URL. Nothing fancy, it just does that!

Useful for situaltions where the main part of your website is located under a sub-folder (i.e. Virtual Dir), and all requests for the root of the site should go to those pages.

Example

You have a webserver configured at http://www.example.com/, but the main part of the site lives at http://www.example.com/pages/. You also have a CRM package installed at http://www.example.com/crm/. You cannot therefore perform a redirect at the IIS level for the whole domain, as the other applications will not be available.

Anyone visiting the plain http://www.example.com/ address is assumed to really want http://www.example.com/pages/, so is directed there, but those who visit the CRM pages directly receive those as expected.

Source Code

Here is the important source code:

protected void Page_Load(object sender, EventArgs e)
{
    string targetURL = ConfigurationManager.AppSettings["TargetApplicationURL"];
    string targetName = ConfigurationManager.AppSettings["TargetApplicationName"];
    Response.Redirect(targetURL, true);
}

The actual URL to redirect to is taken from the web.config file.

The code was tested with ASP.NET 2.0, but should be easily modifiable for version 1.1

0 Comments:

Post a Comment

<< Home