I am in the process of re-writing an old website that was written using a number of different non microsoft technologies. The new site is asp.net web forms.
I have a list of OLD url's in a database and have a routine that will inspect the requested page url against that list of url's in the database, and perform the appropriate 301 redirect to the contents NEW url.
The question I have though, from an efficiency point of view is, how can I perform this OLD url lookup ONLY if the requested Url is essentially a 404/not found. I don't want to fire my 'Check-Old-Urls-And-Redirect-If-Found' routine with every single page request (via Application_BeginRequest in global.asax), as the majority of the time, the requested page will be a valid new .net page that doesn't requires a 301 redirect. Therefore I would like to be able to fire my routine ONLY if it's essentially about to throw a 404 error.
My routine to check the Db for old site Urls is cached, so at least it's not hitting the database every time, but I'd just like to prevent it from even calling this routine (and occasionally hitting the DB when the cache has expired) when it's not necessary.
I was thinking that perhaps I could perform the old url lookup from inside the Application_Error routine in global.asax, checking the error code for 404 status and in that scenario, do the old url lookup, but when testing, it doesn't appear to be hitting my breakpoint in that Application_Error routine in global.asax - instead the browser is showing the IIS 7 404 Not Found error.
It's imperative that the browser receives a 301 response to the new url and doesn't get an incorrect 404 response code (unless the requested url was neither a new or old flavour url).
Can anyone recommend an efficient approach to handling this situation?