URL Re-Writing (and how to FAKE it!)
SingingEels.com has a custom article engine and a custom blog engine which, like many others, uses a database to store the entries, but accesses them through friendly "named" urls such as : http://www.singingeels.com/Articles/Custom_Controls_And_Control_Builders.aspx
Well, someone emailed me (via the Suggestion Box on Eels) asking how I decided to handle that. So here is my reply email that gives away my cheating secret:
(My Email Reply Starts Here >>) Actually, you have a good question that I'll probably blog about later, but for now I'll let you in on my "url re-writing" secret... I cheat :)
When you submit an article on SingingEels (or a blog entry for that matter), I take the article "Title" text and I strip out all non alpha-numeric characters with a Regex. Basically it's as simple as:
string
articleName = Regex.Replace(article.Title, @"[^\w]", "_"); // <-- replace all non-word characters with an underscore.
Then, when I insert the record into the database, I have a field called "ArticleName" to which I will use as the identifyer for the article (so I can pull it up later).
Now the only part that remains is knowing when (and how) to pull up that article and display it when someone comes to my site. Well, the "right" way to do this would be to make your own HttpHandler and skim through each request as it comes... but like I said, I cheat, so what I'm about to say is not the "right" way... it's just the easy way :P
In the "Global.asax" file in your web applications, you can tap into the "Application_Error" event (really it's a method). This method is called whenever there is an error in your application... including 404 errors for pages with the ".aspx" extension.
So, here goes some nasty cheating in ASP.NET:
void Application_Error(object sender, EventArgs e)
{
// Hmmm, an error you say... that's funny, we don't even have
// a page yet! This tells me that it's probably a "404" error
// because the file they requested can't be found.
if (this.Context.Handler == null)
{
// Hey ASP.NET, where *WOULD* the file be if one existed?
string physicalPath = this .Request.PhysicalPath.ToLower();
// Hmmm... that file *WOULD* be in my "Articles" directory!
if (physicalPath.StartsWith(global_asax .articlesDirectoryPath))
{
// Don't worry lil-ASP.NET, I'll vouch for that request...
// and I'll point it to the only real page in my "Articles" directory.
this.Context.Handler = PageParser .GetCompiledPageInstance("~/Articles/Default.aspx",
Server.MapPath("~/Articles/Default.aspx"), HttpContext .Current);
// Oh yeah, and ignore that lil 404 error, would ya?
this.Server.ClearError();
}
}
}
Now, in my "Default.aspx" page in my articles folder, I check (in the "OnLoad" method) if there is an enrty in the database with the requested "articleName" (which I get from removing the extension from the RequestUrl and then strip out the non-alpha-numeric chars).
Then I just populate some basic placeholders and the like with the body of the article (or blog post).
Enjoy :P)