Пример #1
0
        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();
                    }
                }
            }
        }
Пример #2
0
 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();
         }
     }
 }
Пример #3
0
        /// <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);
        }