Show “offline” page in MVC3 while updating database?
I have an MVC3 website backed by a SQL server db. The db gets "refreshed" every night which can take around 30 mins.
During this time, I could take the app offline with an app_offline.htm file (and all of the issues that can cause).
The db refresh is controlled from a windows service, since part of this is to import text files from different sources.
I am wondering if there is a cleaner and nicer way to amend the site so that it shows a friendly "Database updating" type page, without taking the site offline?
A simple way is to add a key the appSettings of your web.config, something like:
<add key="MaintenanceMode" value="true" />
Then you can use the Application_BeginRequest event in the global.asax file and check for the value of the key and redirect to a maintenance page.
I solved this by creating a DatabaseDependant:ActionResultFilterAttribute and applying it to all Action methods that require the DB (or the part of it that gets updated). Applied it to some controllers as well where appropriate.
The filter simply checks a value (used the registry as opposed to app.config as allowed more shared access that the sites app.config) and either redirects or calls the base.