wizard999
10/25/2019 - 1:46 AM

Website Maintenance Middleware

		
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace DickerData.Website.Infrastructure
{
    public class WebsiteMaintenanceMiddleware
    {
        public WebsiteMaintenanceMiddleware(RequestDelegate next)
        {
            _next = next;
        }
        readonly RequestDelegate _next;
        public async Task Invoke(HttpContext context, IConfiguration configuration)
        {
            
            if(IsWebsiteMaintenance(configuration))
            {
                var url = "/Home/WebsiteMaintenance";
                if (context.Request.Path.Value.IndexOf(url, StringComparison.CurrentCultureIgnoreCase) == -1)
                {
                    context.Response.Redirect(url);
                    return;
                }
            }
            await _next(context);
        }
        public static bool IsWebsiteMaintenance(IConfiguration configuration)
        {
            string IsMaintenance = configuration.GetSection("AppConfig")["WebsiteMaintenance"];
            if (IsMaintenance == "1")
            {
                return true;
            }
            return false;
        }
    }
}
            string isUnderMaintenance = Configuration.GetSection("AppConfig")["WebsiteMaintenance"];
            if (isUnderMaintenance == "1" && WebsiteMaintenanceMiddleware.IsWebsiteMaintenance(Configuration))
            {
                app.UseMiddleware<WebsiteMaintenanceMiddleware>();
            }