Created
July 13, 2015 10:19
-
-
Save steven-r/2c5897e907f38efbff9d to your computer and use it in GitHub Desktop.
Handling messages to be displayed between pages with no direct interaction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class SessionMessage | |
{ | |
private const string SessionKey = "TL:MSGLIST"; | |
/// <summary> | |
/// The type of message to be displayed | |
/// </summary> | |
public enum MessageType | |
{ | |
/// <summary> | |
/// Informational message | |
/// </summary> | |
// ReSharper disable once InconsistentNaming | |
info, | |
/// <summary> | |
/// Warning message | |
/// </summary> | |
// ReSharper disable once InconsistentNaming | |
warning, | |
/// <summary> | |
/// General error message | |
/// </summary> | |
Danger, | |
/// <summary> | |
/// Success | |
/// </summary> | |
// ReSharper disable once InconsistentNaming | |
success | |
} | |
/// <summary> | |
/// Add a message to the session | |
/// </summary> | |
/// <param name="message">The message.</param> | |
/// <param name="type">The type.</param> | |
//[Obsolete("Please use AddMessage(MessageType, message)")] | |
public static void AddMessage(string message, MessageType type) | |
{ | |
AddMessageToSession(message, type); | |
} | |
private static void AddMessageToSession(string message, MessageType type) | |
{ | |
Contract.Requires(message != null); | |
Contract.Requires(HttpContext.Current != null); | |
try | |
{ | |
List<SessionEntry> list; | |
HttpSessionState session = HttpContext.Current.Session; | |
if (session == null) | |
{ | |
if (HttpContext.Current.Application[SessionKey] == null) | |
{ | |
HttpContext.Current.Application[SessionKey] = new List<SessionEntry>(); | |
} | |
list = (List<SessionEntry>) HttpContext.Current.Application[SessionKey]; | |
} | |
else | |
{ | |
if (session[SessionKey] == null) | |
{ | |
session[SessionKey] = new List<SessionEntry>(); | |
} | |
list = (List<SessionEntry>) session[SessionKey]; | |
} | |
list.Add(new SessionEntry | |
{ | |
Message = message, | |
MessageType = type | |
}); | |
} | |
catch (Exception ex) | |
{ | |
// In case of any exception just trace the stuff | |
Trace.Fail("Cannot provide message to session", message); | |
Trace.TraceError("Exception was {0}", ex.Message); | |
Trace.Write(ex.StackTrace); | |
} | |
} | |
/// <summary> | |
/// Add a message to the session with a format string. | |
/// </summary> | |
/// <param name="type">The message type to be displayed</param> | |
/// <param name="format">The format to be used. For details please refer to <see cref="string.Format(string, Object[])" /></param> | |
/// <param name="parameters">The parameters.</param> | |
public static void AddMessage(MessageType type, string format, params object[] parameters) | |
{ | |
Contract.Requires(format != null); | |
string message = String.Format(format, parameters); | |
AddMessageToSession(message, type); | |
} | |
/// <summary> | |
/// Add a message to the session with a format string. | |
/// </summary> | |
/// <param name="type">The message type to be displayed</param> | |
/// <param name="message">The message to be displayed/></param> | |
public static void AddMessage(MessageType type, string message) | |
{ | |
AddMessageToSession(message, type); | |
} | |
/// <summary> | |
/// Get a list of session entries. | |
/// </summary> | |
/// <returns> | |
/// The list of session entries. | |
/// </returns> | |
/// <remarks> | |
/// Please note that the list of messages is removed afterwards. | |
/// </remarks> | |
public static List<SessionEntry> GetSessionEntries() | |
{ | |
Contract.Requires(HttpContext.Current != null); | |
Contract.Ensures(Contract.Result<List<SessionEntry>>() != null); | |
List<SessionEntry> resList = new List<SessionEntry>(); | |
HttpSessionState session = HttpContext.Current.Session; | |
HttpApplicationState app = HttpContext.Current.Application; | |
if (app[SessionKey] != null) | |
{ | |
resList.AddRange((List<SessionEntry>)app[SessionKey]); | |
app[SessionKey] = null; | |
} | |
if (session != null && session[SessionKey] != null) | |
{ | |
resList.AddRange((List<SessionEntry>)session[SessionKey]); | |
session[SessionKey] = null; | |
} | |
return resList; | |
} | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
public class SessionEntry | |
{ | |
/// <summary> | |
/// | |
/// </summary> | |
public string Message { get; set; } | |
/// <summary> | |
/// | |
/// </summary> | |
public SessionMessage.MessageType MessageType { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment