From ac1a279d1b67ed4cbb71f9b066c4adac074a1664 Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Wed, 5 Aug 2015 14:03:44 +0100 Subject: [PATCH] #2540: Fixes an issue with NotifyFilter. This fixes the case when a controller for example registers a notification and then returns a View result instead of a Redirect result. When returning a View, the registered notifications would not be rendered until another page request occurred. Fixes #2540 --- src/Orchard/UI/Notify/NotifyFilter.cs | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/Orchard/UI/Notify/NotifyFilter.cs b/src/Orchard/UI/Notify/NotifyFilter.cs index 5b2522614..7961369c7 100644 --- a/src/Orchard/UI/Notify/NotifyFilter.cs +++ b/src/Orchard/UI/Notify/NotifyFilter.cs @@ -61,28 +61,40 @@ namespace Orchard.UI.Notify { public void OnActionExecuted(ActionExecutedContext filterContext) { - // don't touch temp data if there's no work to perform + // Don't touch temp data if there's no work to perform. if (!_notifier.List().Any()) return; + var messageEntries = _notifier.List().ToList(); + + if (filterContext.Result is ViewResultBase) { + // Assign values to the Items collection instead of TempData and + // combine any existing entries added by the previous request with new ones. + var existingEntries = filterContext.HttpContext.Items[TempDataMessages] as IList ?? new List(); + messageEntries = messageEntries.Concat(existingEntries).ToList(); + filterContext.HttpContext.Items[TempDataMessages] = messageEntries; + + return; + } + var tempData = filterContext.Controller.TempData; - // initialize writer with current data + // Initialize writer with current data. var sb = new StringBuilder(); if (tempData.ContainsKey(TempDataMessages)) { sb.Append(tempData[TempDataMessages]); } - // accumulate messages, one line per message - foreach (var entry in _notifier.List()) { + // Accumulate messages, one line per message. + foreach (var entry in messageEntries) { sb.Append(Convert.ToString(entry.Type)) .Append(':') .AppendLine(entry.Message.ToString()) .AppendLine("-"); } - // assign values into temp data - // string data type used instead of complex array to be session-friendly + // Result is not a view, so assume a redirect and assign values to TemData. + // String data type used instead of complex array to be session-friendly. tempData[TempDataMessages] = sb.ToString(); }