Introduction
This is another article that describes the different solutions used to build sharpcms.net. I wrote this to get started with writing the technical descriptions about how we made the solution and to share our ideas with the community. The primary article on CodeProject can be read from here.
For the sharpcms.net project we needed a simple way to rewrite the URLs to get rid of the nasty queries like the ID etc. When we first started the project we wrote a system where we only needed a single query to handle most of the requests. Check out this one.
Therefore, it was a very easy job to make a URL rewrite solution, so the URL would look like this: http://www.sharpcms.net/show/danish/about.aspx.
The solution
We inserted a begin request handler in the global.asax file in the root directory. This handler checked to see if there existed a file with the name requested. In that case, I would not do anything. For example, a request like this: sharpcms.dk/admin.aspx. It would check to see if there existsed a file with that name in the root directory. If not it would rewrite the query to: sharpcms.dk/default.aspx?process=admin.aspx.
This is only rewritten on the server and the client still thinks that it uses the first URL. This is a nice thing because it still looks pretty in the browser. But it has one problem. The browser gets confused because all the relative paths are now incorrect. Therefore, you need to define the base path in HTML header like this:
<html>
<head>
<base href="http://www.sharpcms.dk/">
</head>
</html>
Since our project is an open source project the base path changes from system to system so we wrote a little script to let the server figure out the correct path:
[Editor comment: Line breaks used to avoid scrolling.]
string basepath =
httpPage.Request.ServerVariables["SERVER_PROTOCOL"].
Split('/')[0].ToLower()
+ "://" + httpPage.Request.ServerVariables["SERVER_NAME"]
+ ":" + httpPage.Request.ServerVariables["SERVER_PORT"]
+ httpPage.Request.ApplicationPath.TrimEnd('/')+ "/";
This makes it a lot more dynamic with websites that have more than one domain associated.
Since the .NET Framework on IIS only handles certain extensions like .aspx you have to end the URL with aspx.
The global.asax file
I am not satisfied with the code yet. It looks to me that there should be a nicer or a more cleaner way to do this. I haven't found it yet though. We have tested the code for a while now and it works even with virtual paths.
The code to be placed in the global.asax file is this:
[Editor comment: Line breaks used to avoid scrolling.]
void Application_BeginRequest(object sender, EventArgs e) {
System.Web.HttpContext httpContext = HttpContext.Current;
String currentURL = httpContext.Request.Path.ToLower();
string processPath =
currentURL.Substring(httpContext.Request.ApplicationPath.Length).
TrimStart('/').ToLower();
string physicalPath = httpContext.Server.MapPath(
currentURL.Substring(currentURL.LastIndexOf("/") + 1));
if (!System.IO.File.Exists(physicalPath))
{
string queryString =
httpContext.Request.ServerVariables["QUERY_STRING"];
string defaultPage = "~/default.aspx?process=";
if (processPath.EndsWith(".aspx"))
{
processPath = processPath.Substring(0,
processPath.Length - ".aspx".Length);
}
httpContext.RewritePath(
defaultPage + processPath + "&" + queryString);
}
}