If you don’t have a global.asax in your application, you can create one by choosing "Add New Item" and choosing "Global Application Class". This will provide the class template for handling application events (in file global.asax).
HttpApplication class from the .net framework forms the base class for global.asax. You can take a look at this class to see what events are supported by this class. What we are interested in is BeginRequest. At the beginning of every request we want to see if the current url is a fake one or whether it already exists for real in our system.
To determine if a given url is a fake url, we would need to follow some standards defined by us. For example, all our blogs might follow this standard:
mysite.com/blogs/first-post-from-moon.aspx
To make sure this url is just a fake one, we should NOT have /blogs/ directory on our system (if you have one, you shouldn’t be putting anything that needs to be accessed from outside, of course). That way user could not be asking from some real files in the blogs directory.
So, by searching for the string /blogs/ in the incoming url, we will know that this is a fake url. Other examples could be:
mysite.com/employees/bill-gates.aspx
mysite.com/products/lawn-mower-123.aspx
In all these cases, we need to have simple string that couldn’t be pointing to a real directory (from the examples above: /employees/ or /products/ etc.) Once we determined that it’s a fake url, we need to make sense of the last piece of the url.
So, here the strings first-post-from-moon, bill-gates, and lawn-mower-123 should point to some id. Meaning we need to make the transformations like the following:
mysite.com/blogs/first-post-from-moon.aspx
(to) mysite.com/showblog.aspx?name=first-post-from-moon
(or) mysite.com/showblog.aspx?ID=1234
mysite.com/employees/bill-gates.aspx
(to) mysite.com/displayemp.aspx?id=23-34-34
mysite.com/products/lawn-mower-123.aspx
(to) mysite.com/prods.aspx?prodid=lnmr_123
In these cases, the transformations can be done with the help of some predefined rules (like regular expressions) that convert the first string into the second one. Or, you can go to the database or an xml file to accomplish the conversion process. Start with adding BeginRequest event handler to the global.asax file:
/// <summary>
/// Executed at the beginning of every request.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
///
void Application_BeginRequest(object sender, EventArgs e)
{
// We need to do a url rewrite if the original url contains
// the word articles.
string faker = @"/customers/";
int idx = Request.Path.ToLower().IndexOf(faker);
if (idx != -1)
{
int idxAspx = Request.Path.ToLower().IndexOf(@".aspx");
string fakeName = Request.Path.ToLower().Substring(idx + faker.Length, idxAspx - idx - faker.Length);
// Prepare the new URL
string realPage = "/Customers.aspx?Name=" + fakeName;
string newPath = Request.Path.ToLower().Replace(faker + fakeName + ".aspx", realPage);
// Rewrite the URL
Context.RewritePath(newPath, false);
}
}