public override void OnActionExecuted(System.Web.Http.Filters.HttpActionExecutedContext filterContext) { var request = filterContext.Request; var headers = request.Headers; if (headers.Contains("X-Requested-With") && headers.GetValues("X-Requested-With").FirstOrDefault() == "XMLHttpRequest") { var response = filterContext.ActionContext.Response; if (response != null) { List <SessionMessage> sessionMessages = SessionMessageManager.GetMessage(); if (sessionMessages != null && sessionMessages.Count > 0) { string json = null; var messages = sessionMessages.Where(x => x.Behavior == MessageBehaviors.StatusBar).Select(x => new SessionMessageJsonModal { Message = x.Message, Type = Enum.GetName(typeof(MessageType), x.Type).ToLowerInvariant(), Key = x.Key }).ToList(); if (messages != null && messages.Count > 0) { DataContractJsonSerializer ser = new DataContractJsonSerializer(messages.GetType()); using (MemoryStream ms = new MemoryStream()) { ser.WriteObject(ms, messages); json = Encoding.Default.GetString(ms.ToArray()); ms.Close(); } response.Headers.Add("X-Message", json); } messages = sessionMessages.Where(x => x.Behavior == MessageBehaviors.Modal).Select(x => new SessionMessageJsonModal { Message = x.Message, Type = Enum.GetName(typeof(MessageType), x.Type).ToLowerInvariant(), Key = x.Key }).ToList(); if (messages != null && messages.Count > 0) { DataContractJsonSerializer ser = new DataContractJsonSerializer(messages.GetType()); using (MemoryStream ms = new MemoryStream()) { ser.WriteObject(ms, messages); json = Encoding.Default.GetString(ms.ToArray()); ms.Close(); } response.Headers.Add("X-ModalMessage", json); } SessionMessageManager.Clear(); } } } }
public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext filterContext) { if (!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(HttpContext.Current.User); var requestUrl = filterContext.Request.RequestUri; if (!canBypass && startTime != default(DateTime) && DateTime.UtcNow >= startTime) { if (endTime == default(DateTime) || DateTime.UtcNow <= endTime) { filterContext.Response = new HttpResponseMessage(HttpStatusCode.OK); //response doesn't work string fullyQualifiedUrl = _settingProvider.GetMaintenanceUrl(requestUrl); //response.Headers.Location = new Uri(fullyQualifiedUrl); filterContext.Response.Headers.Add("FORCE_REDIRECT", fullyQualifiedUrl); return; } } if (startTime != default(DateTime) && startTime > DateTime.UtcNow && warningLead > 0) { CookieHeaderValue cookie = filterContext.Request.Headers.GetCookies(KeyMaintenanceWarningCookie).FirstOrDefault(); if (cookie == null || cookie[KeyMaintenanceWarningCookie].Value != "1") { var difference = (startTime - DateTime.UtcNow); if (difference.TotalSeconds < warningLead) { SessionMessageManager.SetMessage(MessageType.Warning, MessageBehaviors.Modal, maintenanceWarningMessage, KeyMaintenanceWarningMessage); CookieHeaderValue newCookie = new CookieHeaderValue(KeyMaintenanceWarningCookie, "1"); newCookie.HttpOnly = true; filterContext.Response.Headers.AddCookies(new CookieHeaderValue[] { newCookie }); } } } } base.OnActionExecuting(filterContext); } }
public override void OnActionExecuted(ActionExecutedContext filterContext) { if (filterContext.HttpContext.Request.IsAjaxRequest()) { var response = filterContext.HttpContext.Response; List <SessionMessage> sessionMessages = SessionMessageManager.GetMessage(); if (sessionMessages != null && sessionMessages.Count > 0) { string json = null; var messages = sessionMessages.Where(x => x.Behavior == MessageBehaviors.StatusBar).Select(x => new SessionMessageJsonModal { Message = x.Message, Type = Enum.GetName(typeof(MessageType), x.Type), Key = x.Key, Caption = x.Caption }).ToList(); if (messages != null && messages.Count > 0) { DataContractJsonSerializer ser = new DataContractJsonSerializer(messages.GetType()); using (MemoryStream ms = new MemoryStream()) { ser.WriteObject(ms, messages); json = Encoding.Default.GetString(ms.ToArray()); ms.Close(); } response.Headers.Add("X-Message", json); } messages = sessionMessages.Where(x => x.Behavior == MessageBehaviors.Modal).Select(x => new SessionMessageJsonModal { Message = x.Message, Type = Enum.GetName(typeof(MessageType), x.Type), Key = x.Key }).ToList(); if (messages != null && messages.Count > 0) { DataContractJsonSerializer ser = new DataContractJsonSerializer(messages.GetType()); using (MemoryStream ms = new MemoryStream()) { ser.WriteObject(ms, messages); json = Encoding.Default.GetString(ms.ToArray()); ms.Close(); } response.Headers.Add("X-ModalMessage", json); } SessionMessageManager.Clear(); } } }
public override void OnActionExecuting(ActionExecutingContext filterContext) { 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 requestUrl = filterContext.HttpContext.Request.Url; if (!canBypass && startTime != default(DateTime) && DateTime.UtcNow >= startTime) { if (endTime == default(DateTime) || DateTime.UtcNow <= endTime) { filterContext.Result = new RedirectResult(_settingProvider.GetMaintenanceUrl(requestUrl)); } } else if (startTime != default(DateTime) && startTime > DateTime.UtcNow && warningLead > 0) { if (filterContext.HttpContext.Request.Cookies[KeyMaintenanceWarningCookie] == null || filterContext.HttpContext.Request.Cookies[KeyMaintenanceWarningCookie].Value != "1") { var difference = (startTime - DateTime.UtcNow); if (difference.TotalSeconds < warningLead) { SessionMessageManager.SetMessage(MessageType.Warning, MessageBehaviors.Modal, maintenanceWarningMessage, KeyMaintenanceWarningMessage); HttpCookie cookie = new HttpCookie(KeyMaintenanceWarningCookie); cookie.Value = "1"; cookie.HttpOnly = true; filterContext.HttpContext.Response.Cookies.Add(cookie); } } } } base.OnActionExecuting(filterContext); }
/// <summary> /// Render all messages that have been set during execution of the controller action. /// </summary> /// <param name="htmlHelper"></param> /// <returns></returns> public string RenderHtml() { var messages = string.Empty; List <SessionMessage> sessionMessages = SessionMessageManager.GetMessage(); TagBuilder messageWrapper = null, messageBoxBuilder = null, messageBoxStatusBar = null, messageBoxModalBuilder = null, messageBoxModal = null; messageWrapper = new TagBuilder("div"); messageWrapper.Attributes.Add("id", "messagewrapper"); messageWrapper.Attributes.Add("style", "display: none"); if (sessionMessages != null && sessionMessages.Count > 0) { for (int i = 0; i < sessionMessages.Count; i++) { var sessionMessage = sessionMessages[i]; switch (sessionMessage.Behavior) { case MessageBehaviors.Modal: if (messageBoxModal == null) { messageBoxModal = new TagBuilder("div"); messageBoxModal.Attributes.Add("id", "messageboxmodal"); } messageBoxModalBuilder = new TagBuilder("div"); //messageBoxModalBuilder.Attributes.Add("id", "messagebox" + i); //messageBoxModalBuilder.Attributes.Add("behavior", ((int)sessionMessage.Behavior).ToString()); messageBoxModalBuilder.AddCssClass(String.Format("messagebox {0}", Enum.GetName(typeof(MessageType), sessionMessage.Type).ToLowerInvariant())); if (!string.IsNullOrEmpty(sessionMessage.Key)) { messageBoxModalBuilder.Attributes.Add("key", sessionMessage.Key); } messageBoxModalBuilder.InnerHtml += sessionMessage.Message; messageBoxModal.InnerHtml += messageBoxModalBuilder.ToString(); break; case MessageBehaviors.StatusBar: default: if (messageBoxStatusBar == null) { messageBoxStatusBar = new TagBuilder("div"); messageBoxStatusBar.Attributes.Add("id", "messageboxstatusbar"); } messageBoxBuilder = new TagBuilder("div"); //messageBoxBuilder.Attributes.Add("id", "messagebox" + i); messageBoxBuilder.Attributes.Add("behavior", (sessionMessage.Behavior).ToString()); messageBoxBuilder.Attributes.Add("type", (sessionMessage.Type).ToString()); messageBoxBuilder.Attributes.Add("caption", sessionMessage.Caption); messageBoxBuilder.AddCssClass(String.Format("messagebox {0}", Enum.GetName(typeof(MessageType), sessionMessage.Type).ToLowerInvariant())); if (!string.IsNullOrEmpty(sessionMessage.Key)) { messageBoxBuilder.Attributes.Add("key", sessionMessage.Key); } messageBoxBuilder.InnerHtml += sessionMessage.Message; messageBoxStatusBar.InnerHtml += messageBoxBuilder.ToString(); break; } } messages = messageBoxStatusBar == null || string.IsNullOrEmpty(messageBoxStatusBar.ToString()) ? null : messageBoxStatusBar.ToString(); messages += messageBoxModal == null || string.IsNullOrEmpty(messageBoxModal.ToString()) ? null : messageBoxModal.ToString(); messageWrapper.InnerHtml += messages; SessionMessageManager.Clear(); } messages = messageWrapper.ToString(); return(messages); }
public ActionResult AjaxMessage(SessionMessageViewModel vm) { SessionMessageManager.SetMessage(vm.Type, vm.Behaviors, vm.Message); return(View("Index")); }
public ActionResult Index(SessionMessageViewModel vm) { ViewBag.Message = "Message page."; SessionMessageManager.SetMessage(vm.Type, vm.Behaviors, vm.Message); return(View(vm)); }