Implement "Down for maintenance" page
Asked Answered
W

5

11

I know we could simply use an app_offline.htm file to do this.

But I want to be able access the website if my IP is 1.2.3.4 (for example), so that I can do a final testing.

if( IpAddress != "1.2.3.4" )
{
    return Redirect( offlinePageUrl );
}

How can we implement this in ASP.NET MVC 3?

Westley answered 28/9, 2011 at 9:13 Comment(4)
You can still do this only using IIS. Set up a new site with a different host name binding that doesn't redirect. No need to taint your code with "deployment" stuff. :)Eatable
@Eatable we already done testing with different host name. we need to do a final testing again with real host name.Westley
I can see that happening as well.Eatable
I have an MVC app on Azure IIS and have realized app_offline.htm for MVC might not be the best option. I quickly used app_offline.htm but stylesheets, images, and script files that are referenced could break. For example, IE Edge forces the slash (/) after the end of the domain whereas google chrome does not include the slash. My references broke. For MVC, I think routes need to take charge and not the app_offline.htmCytolysis
S
14

You can use a catch-all route with a RouteConstraint with the IP check:

Make sure you put the offline route first.

routes.MapRoute("Offline", "{controller}/{action}/{id}",
                new
                    {
                        action = "Offline",
                        controller = "Home",
                        id = UrlParameter.Optional
                    },
                new { constraint = new OfflineRouteConstraint() });

and the constraint code:

public class OfflineRouteConstraint : IRouteConstraint
{
    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        // return IpAddress != "1.2.3.4";
    }
}
Selfridge answered 28/9, 2011 at 10:53 Comment(2)
Very clean solution. Does exactly what I needed. Thanks.Arella
@Rickard, users on live shopping site get effected with error when i upload .DLL to update code changes in project, so will down for maintenance help ? or statuspage.io paid service ? here is discussion on similar issueSeparator
U
14

Per Max's suggestion here is an actual implementation.

public class MvcApplication : System.Web.HttpApplication
{

    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new CheckForDownPage());

    }

    //the rest of your global asax
    //....
}
public sealed class CheckForDownPage : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var path = System.Web.Hosting.HostingEnvironment.MapPath("~/Down.htm");

        if (System.IO.File.Exists(path) && IpAddress != "1.2.3.4")
        {
            filterContext.HttpContext.Response.Clear();
            filterContext.HttpContext.Response.Redirect("~/Down.htm");
            return;
        }

        base.OnActionExecuting(filterContext);
    }


}
Unthoughtof answered 4/5, 2012 at 14:43 Comment(0)
F
2

You can define a global filter that stop all the requests if they don't come from your IP. you can enable the filter by configuration.

Frayne answered 28/9, 2011 at 12:41 Comment(0)
H
2

I got an infinite loop on colemn615's solution, so I added a check for the offline page.

Also, for later versions of ASP.NET this is split into a FilterConfig.cs file in the App_Start folder.

public class FilterConfig
{

    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new CheckForDownPage());

    }

    public sealed class CheckForDownPage : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (HttpContext.Current.Request.RawUrl.Contains("Down.htm"))
            {
                return;
            }

            var path = System.Web.Hosting.HostingEnvironment.MapPath("~/Down.htm");

            if (System.IO.File.Exists(path) && IpAddress != "1.2.3.4")
            {
                filterContext.HttpContext.Response.Clear();
                filterContext.HttpContext.Response.Redirect("~/Down.htm");
                return;
            }

        base.OnActionExecuting(filterContext);
    }
}
Hickox answered 30/10, 2015 at 23:43 Comment(0)
A
1

I add an AppSetting in the Web.Config file:

<add key="MaintenanceMsg" value="We are currently performing some routine maintenance. We expect to be back up at around 01:00." />

I then update the global.asax file's Application_BeginRequest method to check if the appsetting has a value, if it does, I redirect everything to the maintenance page:

private void Application_BeginRequest(object sender, EventArgs e)
{
        if(!string.IsNullOrWhiteSpace(ConfigurationManager.AppSettings["MaintenanceMsg"]) && !Request.Url.ToString().Contains("UndergoingMaintenance"))
            Response.Redirect("~/Home/UndergoingMaintenance");

 ** Do whatever you do when you don't want to show your maintenance page here**
}

Finally, create your view and controller action.

If you use Azure you can simply add and delete the value for the AppSetting in the portal so you can suspend your site in a matter of minutes without a deployment.

Aschim answered 19/1, 2022 at 22:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.