Make Custom URLs in ASP.NET with the Global.asax File

If you’re looking to do URL rewriting for a content management system or another .NET based website, there are a number of ways to accomplish the task. There’s a very mature free product called URL Rewriter.NET which gets the job done, and Microsoft has made their own URL Routing solution which was originally developed for their MVC framework, however it works for those of us that still use regular old web forms too.

These types of solutions do a very good job in mapping URLs that follow a very standardized methodology, say if you wanted to www.example.org/pages/15/, it would be very easy to do with either of those solutions. However, those standardized packages don’t necessarily do the best job when you want to do URL rewriting that doesn’t have any identifying information. Let’s say we wanted to do www.example.org/about/subpage/. There’s no Page ID that can be taken out of that other than the URL. How do we map that to the corresponding page in our content management system?

The solution I’ve developed uses ASP.NET Application File (global.asax) to query a table in a SQL Database that has a list of PageID’s, their URL Aliases (i.e. /about/), and then the page’s content and title.

Here’s the code:

protected void Application_BeginRequest(Object sender, EventArgs e)
{

HttpContext incoming = HttpContext.Current;
string oldpath = incoming.Request.Path.ToLower();
string pageid;

//get information about the associated section
string CurrentPath = oldpath;
if (!CurrentPath.StartsWith(“/”)) { CurrentPath = “/” + CurrentPath; }

SqlCommand cmdSqlCommand = new System.Data.SqlClient.SqlCommand(“select top 1 ID, URLAlias from Pages where URLAlias = @URLAlias”);
cmdSqlCommand.Parameters.AddWithValue(“@URLAlias”, CurrentPath);
DataTable dsResults = DBUtils.SQLSelect(cmdSqlCommand);

//if a match was found, perform the rewrite
if (dsResults.Rows.Count > 0)
{
DataRow thisPage = dsResults.Rows[0];
incoming.RewritePath(HttpRuntime.AppDomainAppVirtualPath + “” + “Default.aspx?id=” + PageID);
}

}

Basically, the code takes a look at the relative path that it’s browsing to, checks against th database and see if there’s a dynamic page that has that relative URL, and if it does, it’ll redirect to an ASP.NET Page which will grab the appropriate content from the database based on the ID that it converts to. In the URL, you’ll see the custom URL that you want to use, but the ASP.NET Page will be able to access the query string to get content from the database just like it would have if you were visiting the actual page. You’ll not that the code above uses my “Dbutils” library seen in this post, but you’re welcome to query your table any awy that you’d like!

The main benefit of keeping a table of relative paths that you want to map is that you don’t have to follow any specific conventions, such as “/content/48/”  that you might see in Joomla, but instead can have some very good looking urls such as http://www.example.org/about/ or http://www.example.org/events/my-event/. You might need to make a few configuration changes on your website in IIS if you’re doing this, but it can be done in both IIS 6 and IIS 7.

Speak Your Mind

*