ASP.NET version | Package |
---|---|
ASP.NET Core | |
ASP.NET Classic |
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 Core NuGet package and import the Vereyon.Web
namespace where you need it.
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>();
Typically you want to render all queued flash messages in your Layout Razor template using the following code:
@Vereyon.Web.FlashMessageHtmlHelper.RenderFlashMessages(Html)
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");
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.