Skip to content

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

License

Notifications You must be signed in to change notification settings

Vereyon/FlashMessage

Repository files navigation

FlashMessage

ASP.NET version Package
ASP.NET Core 2 and up Nuget version
ASP.NET Classic and Core 1 NuGet version

FlashMessage provides easy cross request notifications for ASP.NET MVC based on Twitter Bootstrap 3 and up. It solves the problem with flashing the user a notification or message when using the Post/Redirect/Get pattern and RedirectToAction() method.

Flash Message example!

Quickstart

Install the FlashMessage NuGet package :

Install-Package Vereyon.Web.FlashMessage

Register the flash message service in your startup class:

// Add services required for flash message to work.
services.AddFlashMessage();

Inject IFlashMessage in your controllers:

public HomeController(IFlashMessage flashMessage)

Queue some messages in your action method:

_flashMessage.Confirmation("Your confirmation message");

Register the tag helper in your view and have the messages rendered using <flash />:

@addTagHelper *, Vereyon.Web.FlashMessage
<flash dismissable="true" />

Upgrading to version 2.0

When updating from FlashMessage 1.x, the following changes need to be taken into account:

  • FlashMessage now only supports .NET Standard 2.0 and up.
  • Some of the short hand methods to queue messages have been changed:
  • The Queue() methods have all been removed, except for the one taking a FlashMessageModel instance.
  • Methods accepting a string format args have removed.
  • Consistently put message parameter first, title second.
  • The HtmlHelper has been replaced with a Razor tag helper.

Usage

ASP.NET Core 2 and up

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

Install-Package Vereyon.Web.FlashMessage

Registering required services for depencency injection

Register the required services during startup of you application:

// Add services required for flash message to work.
services.AddFlashMessage();

Rendering flash messages

Typically you'll want to render all queued flash messages in your Layout Razor template. For this purpose a tag helper is available. Register the tag helper in your view as follows:

@addTagHelper *, Vereyon.Web.FlashMessage

Use the tag helper by writing <flash />. Optionally you can indicate the messages should be dismiss by setting dismissable="true":

<flash dismissable="true" />

By default, messages will be rendered for compatibility with Twitter Bootstrap 5. In case a different Bootstrap version is desired, this can be set using the bootstrap-version attribute:

<flash bootstrap-version="3" />

Queuing flash messages

In order to be able to queue flash messages you'll need a reference to an instance of 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: {user.Name}");
return RedirectToLocal(returnUrl);

Different types of messages can be queued using different methods on the IFlashMessage interface:

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 Core 1

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

Install-Package Vereyon.Web.FlashMessage

Registering required services for depencency injection

Register the required services during startup of you application:

// Add services required for flash message to work.
services.AddFlashMessage();

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)

Optionally you can disable the dismiss icon passing dismissable: false:

@Vereyon.Web.FlashMessageHtmlHelper.RenderFlashMessages(Html, false)

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 methods on the IFlashMessage interface:

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

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •