SharpGrip FlashMessages is a messaging system for adding and reading flash messages.
Reference NuGet package SharpGrip.FlashMessages
(https://www.nuget.org/packages/SharpGrip.FlashMessages).
Add the FlashMessages messaging system to your service collection via the extension method.
public void ConfigureServices(IServiceCollection services)
{
services.AddFlashMessages();
}
Property | Default value | Description |
---|---|---|
StorageKey | __FlashMessages__ |
The key used to store message data in the ITempDataDictionary session storage. |
using SharpGrip.FlashMessages.Extensions;
using SharpGrip.FlashMessages.Options;
public void ConfigureServices(IServiceCollection services)
{
services.AddFlashMessages(options =>
{
options.StorageKey = "SomeOtherStorageKey";
});
}
using SharpGrip.FlashMessages.Extensions;
using SharpGrip.FlashMessages.Options;
public void ConfigureServices(IServiceCollection services)
{
services.AddFlashMessages();
services.Configure<FlashMessagesOptions>(options =>
{
options.StorageKey = "SomeOtherStorageKey";
});
}
Inject the IFlasher
service and call the IFlasher.Add(messageType, message)
method.
flasher.Add("success", "My very first success message!");
flasher.Add("info", "My very first info message!");
flasher.Add("warning", "My very first warning message!");
flasher.Add("danger", "My very first danger message!");
Inject the IFlasher
service and call the IFlasher.GetMessages()
method.
@using SharpGrip.FlashMessages
@inject IFlasher Flasher
<div class="alerts">
@foreach (var message in Flasher.GetMessages())
{
<div class="row alert [email protected] alert-dismissible fade show" role="alert">
@message.Text
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
}
</div>
Inject the IFlasher
service and call the IFlasher.PeekMessages()
method. Peeking at messages will not clear the messages present in the message store.
var messages = flasher.PeekMessages();
Inject the IFlasher
service and call the IFlasher.Clear()
method.
flasher.Clear();
Note: Clearing the message store is not necessary when reading messages via the IFlasher.GetMessages()
method.