FlashMessage provides easy cross request notifications for ASP.NET MVC based on Twitter Bootstrap. It solves the problem with flashing the user a notification or message when using the Post/Redirect/Get pattern and RedirectToAction()
method.
Install the FlashMessage NuGet package and import the Vereyon.Web
namespace where you need it.
Typically you want to render all queued flash messages in your Layout Razor template using the following code:
@Html.RenderFlashMessages()
Queuing a confirmation message for display on the next request after for example user login is done as follows:
// User successfully logged in
FlashMessage.Confirmation("You have been logged in as: {0}", user.Name);
return RedirectToLocal(returnUrl);
Different types of messages can be scheduled using different static methods on the FlashMessage object:
FlashMessage.Info("Your informational message");
FlashMessage.Confirmation("Your confirmation message");
FlashMessage.Warning("Your warning message");
FlashMessage.Danger("Your danger alert");
FlashMessage.Danger("Message title", "Your danger alert");
Using the FlashMessage.Queue() method advanced options are available:
FlashMessage.Queue(string.Format("You have been logged in as: {0}", user.Name), "Title", FlashMessageType.Confirmation, false);
The FlashMessage
class allows you to queue messages anywhere in your code where a HttpContext is available and the response has not yet been sent out. You can thus also use FlashMessage outside your MVC actions or with WebForms applications.
Got tests? Yes, see the tests project. It uses xUnit.