using System;
using System.Web;
using System.IO;
namespace AbstractUrl
{
///
/// Implements a simple Abstract URL resolver.
/// By aegrumet@alum.mit.edu on 2003-06-27.
///
/// To install this code, you must compile it to the bin directory in your
/// web site root or virtual directory.
///
/// You must also add an entry to the httpModules section of the web.config file
/// in your web site root or virtual directory. If the file does not exist,
/// you can create one containing the following:
///
///
///
///
///
///
///
///
///
/// Here we assume that your compiled code resides in bin\AbstractUrl.dll.
///
/// Finally, you must modify the IIS configuration for your web site or virtual
/// directory, so that the ASP.NET engine will be invoked for extensionless
/// requests. In the Internet Information Services manager, right-click on
/// the site or virtual directory and select Properties. Navigate to the
/// Home Directory tab (for a web site) or the Virtual Directory tab (for a
/// virtual directory) and click Configuration...
///
/// IIS6: insert a wildcard application map that points to
/// C:\WINDOWS\Microsoft.NET\Framework\vX.Y.Z\aspnet_isapi.dll.
/// Make sure that "verify that file exists" is unchecked.
///
/// IIS5: add an application mapping. In the add/edit application extension
/// mapping window, enter
/// C:\WINDOWS\Microsoft.NET\Framework\vX.Y.Z\aspnet_isapi.dll
/// for the Executable. Hint: if you're using the Browse... window you want
/// "Files of type" to be set to "Dynamic Link libraries (*.dll)".
/// Enter .* ("dot star") for the Extension.
/// VERY IMPORTANT: Due to a bug in IIS, the OK button will probably be greyed out.
/// Click your mouse in the white text area for the Executable field. This should
/// cause the OK button to become clickable. Make sure that "Check that file exists"
/// is unchecked. Click OK.
///
///
public class AbstractUrlModule : IHttpModule
{
///
/// This is our hook into the HttpApplication event model.
/// Here we register our OnBeginRequest event handler.
///
/// The containing web application object.
public void Init(HttpApplication httpApp)
{
httpApp.BeginRequest += new EventHandler(this.OnBeginRequest);
}
///
/// Disposes of the resources used by instances of this type.
///
public void Dispose() {}
///
/// Implements a simple version of abstract URLs. We won't use
/// either of the arguments, which are generic to all event handlers.
///
/// The object that raised this event.
/// Generic bag of arguments.
public void OnBeginRequest(object sender, EventArgs e)
{
if (HttpContext.Current.Request.Path.IndexOf(".") >= 0)
{
//The request has an extension, so just return.
return;
}
//Add ".aspx" to the computed physical file location.
string strTestPhysicalPath = String.Concat(
HttpContext.Current.Request.PhysicalPath,
".aspx"
);
if (File.Exists(strTestPhysicalPath))
{
string strNewPath = String.Concat(
HttpContext.Current.Request.Path,
".aspx"
);
//A matching file exists. Rewrite the request path.
HttpContext.Current.RewritePath(strNewPath);
}
//No special action if we didn't find a match. IIS will return
//a 404 Not Found for us.
return;
}
}
}