HttpHandlers and the IIS Metabase.
Our team has recently consolidated a bunch of intranet sites housed on various team members' machines into subwebs on a larger server. I had been hosting one of the sites from my office, and needed a way to redirect all hyperlinks to that particular subweb to a new subweb without affecting the rest of the sites on the machine. ASP.NET provides a rather simple way of doing this:
using System.Web;
namespace Forward {
public class Forwarder : IHttpHandler
{
public Forwarder(){}
public void ProcessRequest(HttpContext context)
{
string subWeb = "http://mymachine/subweb";
string oldWeb = "http://newmachine/newsubweb";
HttpRequest request = context.Request;
HttpResponse response = context.Response;
string newUrl = request.Url.AbsoluteUri.Replace(subWeb,newWeb);
response.Redirect(newUrl);
}
public bool IsReusable
{
get { return false; }
}
}
}
compiled as forward.dll and placed in the subweb's bin/ directory, with the following added to the Web.config:
<httpHandlers>
<add verb="*" path="*" type="Forward.Forwarder, Forward" />
</httpHandlers>
Funny enough, this only worked for paths like http://mymachine/subweb/foo.aspx, since paths like http://mymachine/subweb/foo.htm were routed directly to the filesystem by IIS and bypassed ASP.NET. I remembered having this problem before, when .NET was still in development, and the trick was to associate the "*" file extension with ASP.NET in the script map. Too bad IIS no longer stores the script map in the registry where it's easy to mess with.
A bit of searching and I found:
http://support.microsoft.com/default.aspx?scid=KB;en-us;q232068#3
I just had to use MetaEdit to find LM/W3SVC/1/ROOT/ScriptMaps and add:
*,C:\WINDOWS\Microsoft.NET\Framework\v1.2.x86dbg\aspnet_isapi.dll,1
and everything is working great now. [Better Living Through Software]