Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Just for fun, I created my own custom error (message) handler that works pretty much as TempData, but with the small difference that this handler is accessible all over the application. </p> <p>I'm not going to explain every single step of code, but to sum it all up, I used IHttpModule to fire a method for every request and Session to save data. Below is the code, feel free to edit or give suggestions for improvements.</p> <p><strong>Web.config</strong> (Define module)</p> <pre><code>&lt;httpModules&gt; &lt;add name="ErrorManagerModule" type="ErrorManagerNamespace.ErrorManager"/&gt; &lt;/httpModules&gt; &lt;system.webServer&gt; &lt;modules runAllManagedModulesForAllRequests="true"&gt; &lt;add name="ErrorManagerModule" type="ErrorManagerNamespace.ErrorManager"/&gt; &lt;/modules&gt; &lt;/system.webServer&gt; </code></pre> <p><strong>ErrorManager.cs</strong> (Error manager handler code)</p> <pre><code>public class ErrorManager : IRequiresSessionState, IHttpModule { private const string SessionKey = "ERROR_MANAGER_SESSION_KEY"; public enum Type { None, Warning, Success, Error } /* * * Public methods * */ public void Dispose() { } public void Init(HttpApplication context) { context.AcquireRequestState += new EventHandler(Initiliaze); } public static IList&lt;ErrorModel&gt; GetErrors(ErrorManager.Type type = Type.None) { // Get all errors from session var errors = GetErrorData(); // Destroy Keep alive // Decrease all errors request count foreach (var error in errors.Where(o =&gt; type == ErrorManager.Type.None || o.ErrorType == type).ToList()) { error.KeepAlive = false; error.IsRead = true; } // Save errors to session SaveErrorData(errors); //return errors; return errors.Where(o =&gt; type == ErrorManager.Type.None || o.ErrorType == type).ToList(); } public static void Add(ErrorModel error) { // Get all errors from session var errors = GetErrorData(); var result = errors.Where(o =&gt; o.Key.Equals(error.Key, StringComparison.OrdinalIgnoreCase)).FirstOrDefault(); // Add error to collection error.IsRead = false; // Error with key is already associated // Remove old error from collection if (result != null) errors.Remove(result); // Add new to collection // Save errors to session errors.Add(error); SaveErrorData(errors); } public static void Add(string key, object value, ErrorManager.Type type = Type.None, bool keepAlive = false) { // Create new error Add(new ErrorModel() { IsRead = false, Key = key, Value = value, KeepAlive = keepAlive, ErrorType = type }); } public static void Remove(string key) { // Get all errors from session var errors = GetErrorData(); var result = errors.Where(o =&gt; o.Key.Equals(key, StringComparison.OrdinalIgnoreCase)).FirstOrDefault(); // Error with key is in collection // Remove old error if (result != null) errors.Remove(result); // Save errors to session SaveErrorData(errors); } public static void Clear() { // Clear all errors HttpContext.Current.Session.Remove(SessionKey); } /* * * Private methods * */ private void Initiliaze(object o, EventArgs e) { // Get context var context = ((HttpApplication)o).Context; // If session is ready if (context.Handler is IRequiresSessionState || context.Handler is IReadOnlySessionState) { // Load all errors from session LoadErrorData(); } } private static void LoadErrorData() { // Get all errors from session var errors = GetErrorData().Where(o =&gt; !o.IsRead).ToList(); // If KeepAlive is set to false // Mark error as read foreach (var error in errors) { if (error.KeepAlive == false) error.IsRead = true; } // Save errors to session SaveErrorData(errors); } private static void SaveErrorData(IList&lt;ErrorModel&gt; errors) { // Make sure to remove any old errors HttpContext.Current.Session.Remove(SessionKey); HttpContext.Current.Session.Add(SessionKey, errors); } private static IList&lt;ErrorModel&gt; GetErrorData() { // Get all errors from session return HttpContext.Current.Session[SessionKey] as IList&lt;ErrorModel&gt; ?? new List&lt;ErrorModel&gt;(); } /* * * Model * */ public class ErrorModel { public string Key { get; set; } public object Value { get; set; } public bool KeepAlive { get; set; } internal bool IsRead { get; set; } public Type ErrorType { get; set; } } </code></pre> <p><strong>HtmlHelperExtension.cs</strong> (An extension method for rendering the errors)</p> <pre><code>public static class HtmlHelperExtension { public static string RenderMessages(this HtmlHelper obj, ErrorManager.Type type = ErrorManager.Type.None, object htmlAttributes = null) { var builder = new TagBuilder("ul"); var errors = ErrorManager.GetErrors(type); // If there are no errors // Return empty string if (errors.Count == 0) return string.Empty; // Merge html attributes builder.MergeAttributes(new RouteValueDictionary(htmlAttributes), true); // Loop all errors foreach (var error in errors) { builder.InnerHtml += String.Format("&lt;li class=\"{0}\"&gt;&lt;span&gt;{1}&lt;/span&gt;&lt;/li&gt;", error.ErrorType.ToString().ToLower(), error.Value as string); } return builder.ToString(); } } </code></pre> <p><strong>Usage for creating errors</strong></p> <pre><code>// This will only be available for one request ErrorManager.Add("Key", "An error message", ErrorManager.Type.Error); // This will be available for multiple requests // When error is read, it will be removed ErrorManager.Add("Key", "An error message", ErrorManager.Type.Error, true); // Remove an error ErrorManager.Remove("AnotherKey"); // Clear all error ErrorManager.Clear(); </code></pre> <p><strong>Usage for rendering errors</strong></p> <pre><code>// This will render all errors &lt;%= Html.RenderMessages() %&gt; // This will just render all errors with type "Error" &lt;%= Html.RenderMessages(ErrorManager.Type.Error) %&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload