What's a good setup for handling session / "flash-style" messages in ASP.NET?
I am building ASP.NET 2.0 websites, and currently I have put together a few homebrews for DALs, some business objects, and various other common stuff. I've put together a basic set of objects to handle messages in the UI (i.e. Errors and other status messages). It consists of:
-
StatusMessage , which has a text property and a color property to describe the message to display. It also has a static dispatch method that instantiates on its own and puts it in a session variable.
-
StatusMessageDisplay , which is basically a Label object with an overridden Refresh method to grab the StatusMessage from the session, clear the Session variable, and display the message.
Overall, it worked pretty well. However, there are a few things that I don't like:
-
It can only handle one message at a time, if two messages are sent before they are displayed, the first one is lost. I could compose a list of messages to display, but I'm not sure if I want to start creating lists in the Session object.
-
It is bound to the session and therefore to the HttpContext. I try not to nest much in the Session object and the HttpContext is hard work for testing. I am currently wrapping the HttpContext in another object that I can mock for testing.
-
In my current implementation, if a message is passed from an AJAX request, the message is displayed later (next request sync'd) because the StatusMessageDisplay control does not support / trigger AJAX.
Before I start building on what I need to fix, I would like to know what others have found useful to do this. What I really really like is something that behaves like a Flash object in Rails, or maybe a more appropriate alternative?
source to share