Esempio n. 1
0
 public ActionResult Index(SessionMessageViewModel vm)
 {
     ViewBag.Message = "Message page.";
     _sessionMessageManager.SetMessage(vm.Type, vm.Behaviors, vm.Message);
     //return RedirectToAction("About");
     return(View(vm));
 }
Esempio n. 2
0
        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var  maintenanceAttribute = filterContext.ActionDescriptor.FilterDescriptors.Select(x => x.Filter).OfType <MaintenanceAttribute>().FirstOrDefault();
            bool disabled             = false;

            if (maintenanceAttribute != null)
            {
                disabled = maintenanceAttribute.Disabled;
            }
            if (!disabled)
            {
                if (_settingProvider == null)
                {
                    throw new Exception("IMaintenanceSettingProvider must be provided when Disabled is false.");
                }
                var  startTime   = _settingProvider.StartTimeUtc;
                var  endTime     = _settingProvider.EndTimeUtc;
                var  warningLead = _settingProvider.WarningLeadTime;
                var  maintenanceWarningMessage = _settingProvider.MaintenanceWarningMessage;
                bool canBypass = _settingProvider.CanByPass(filterContext.HttpContext.User);
                var  request   = filterContext.HttpContext.Request;
                if (!canBypass && startTime != default(DateTime) && DateTime.UtcNow >= startTime)
                {
                    if (endTime == default(DateTime) || DateTime.UtcNow <= endTime)
                    {
                        filterContext.Result = new RedirectResult(_settingProvider.GetMaintenanceUrl(request));
                    }
                }
                else if (startTime != default(DateTime) && startTime > DateTime.UtcNow && warningLead > 0)
                {
                    if (filterContext.HttpContext.Request.Cookies[KeyMaintenanceWarningCookie] == null || filterContext.HttpContext.Request.Cookies[KeyMaintenanceWarningCookie] != "1")
                    {
                        var difference = (startTime - DateTime.UtcNow);
                        if (difference.TotalSeconds < warningLead)
                        {
                            //string baseUrl = string.Format("{0}://{1}{2}", filterContext.HttpContext.Request.Scheme, filterContext.HttpContext.Request.Host, filterContext.HttpContext.Request.PathBase);
                            //        string script = @"$.ajax({{
                            //url: '{0}/api/MaintenanceFilter/MarkShowed',
                            //method: ""post""
                            //    }}); ";
                            _sessionMessageManager.SetMessage(MessageType.Warning, MessageBehaviors.Modal, maintenanceWarningMessage, KeyMaintenanceWarningMessage);//, string.Format(script, baseUrl));
                            filterContext.HttpContext.Response.Cookies.Append(KeyMaintenanceWarningCookie, "1", new CookieOptions {
                                HttpOnly = true
                            });
                        }
                    }
                }
            }
        }