Implement a "maintenance mode" in asp.net core

Viewed 3477

How could you implement a site-wide "Maintenance mode" for your Asp.Net Core app?

Requirements:

  • All controller requests respond with the same static "Maintenance mode" page
  • You need to be able to flip a switch in the backend to turn maintenance mode on and off (so it has to happen at runtime)

My current best idea is an actionfilter on all controllers, where I check for the maintenance mode in code (bit in a database) and just return the maintenance mode page if maintenance mode is true.

2 Answers

The best way to do that. You can use environment variable in your application, But that you must be need to be modified VariableName whenever you need to turn off maintenance.

"IIS Express (Staging)": {
  "commandName": "IISExpress",
  "launchBrowser": true,
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Staging"
  }
}

Look at above code is from launchSettings.json files. which contains set ASPNETCORE_ENVIRONMENT as a Staging.

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
   if (env.IsDevelopment())
   {
       app.UseDeveloperExceptionPage();
       app.UseDatabaseErrorPage();
       app.UseBrowserLink();
   }
   else
   {
       app.UseExceptionHandler("/Home/Maintainance");
   }
}

Even you could also be change some content in your layout.cshtml page as per your environment variable.

<environment names="Development">
    <link rel="stylesheet" href="<your-regular-css-files>" />
</environment>
<environment names="Staging,Production">
    <link rel="stylesheet" href="<your-maintainance-css-files>" />
</environment>

But Here is your one requirement is that to, you should need to be able to change maintenance mode on backed side. In this scenario change JSON file ASPNETCORE_ENVIRONMENT value from your admin panel. I think this should be work fine.

Related