Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save danielgreen/5671345 to your computer and use it in GitHub Desktop.
Save danielgreen/5671345 to your computer and use it in GitHub Desktop.
A filter (which should be used as a global filter) to check whether the application is configured as offline, and throw an exception if this is the case. This is an alternative to placing a file named App_Offline.htm in the application's root. Using App_Offline.htm affects all users, cannot be selectively bypassed as in this case, and does not a…
using System;
using System.Net;
using System.Web.Mvc;
using System.Web.Routing;
namespace Web.Filters
{
/// <summary>
/// Check whether the application is marked as offline in the config file.
/// If so, and the user does not have the override permission, throw an exception.
/// The MVC error handling will deal with displaying the exception message to the user.
/// </summary>
public class CheckApplicationOfflineAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
// Only need to carry out the check on parent actions, otherwise there is an unnecessary overhead
if (filterContext.IsChildAction)
return;
if (ConfigurationHelper.ApplicationOffline && !SecurityHelper.UserHasApplicationOfflineOverridePermission)
throw new OfflineException("The system is offline for maintenance.");
}
}
}
// Apply a filter so that Elmah does not log any "system offline" exceptions. Otherwise the log will
// be flooded with those messages as users try to access the system while it is down for maintenance.
void ErrorLog_Filtering(object sender, Elmah.ExceptionFilterEventArgs e)
{
if (e.Exception is OfflineException)
e.Dismiss();
}
using System;
namespace Utility
{
public class OfflineException : ApplicationException
{
public OACSOfflineException(string message)
: base(message)
{
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment