/FlashMessage

FlashMessage provides easy cross request notifications for ASP.NET MVC based on Twitter Bootstrap alerts.

Primary LanguageC#MIT LicenseMIT

FlashMessage

ASP.NET version Package
ASP.NET Core NuGet version
ASP.NET Classic NuGet version

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.

Usage

ASP.NET Core

Install the FlashMessage Core NuGet package and import the Vereyon.Web namespace where you need it.

Registering required services for depencency injection

Register the required services during startup of you application:

services.AddSingleton<ITempDataProvider, CookieTempDataProvider>();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddScoped<Vereyon.Web.IFlashMessage, Vereyon.Web.FlashMessage>();

Rendering flash messages

Typically you want to render all queued flash messages in your Layout Razor template using the following code:

@Vereyon.Web.FlashMessageHtmlHelper.RenderFlashMessages(Html)

Queuing flash messages

In order to be able to queue flash messages you'll need a reference to the IFlashMessage interface. Ask for it to be injected in your controller:

public HomeController(IFlashMessage flashMessage)

Queuing a confirmation message for display on the next request after for example user login is done as follows, assuming that FlashMessage is a property of type IFlashMessage:

// 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");

ASP.NET 5 and earlier

Install the FlashMessage NuGet package and import the Vereyon.Web namespace where you need it.

Rendering flash messages

Typically you want to render all queued flash messages in your Layout Razor template using the following code:

@Html.RenderFlashMessages()

Queuing flash messages

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");

Advanced options

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.

Tests

Got tests? Yes, see the tests project. It uses xUnit.

More information

License

MIT X11