Sapient.AspNetCore.FlashMessages 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Sapient.AspNetCore.FlashMessages --version 1.0.0
NuGet\Install-Package Sapient.AspNetCore.FlashMessages -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Sapient.AspNetCore.FlashMessages" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Sapient.AspNetCore.FlashMessages --version 1.0.0
#r "nuget: Sapient.AspNetCore.FlashMessages, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Sapient.AspNetCore.FlashMessages as a Cake Addin
#addin nuget:?package=Sapient.AspNetCore.FlashMessages&version=1.0.0

// Install Sapient.AspNetCore.FlashMessages as a Cake Tool
#tool nuget:?package=Sapient.AspNetCore.FlashMessages&version=1.0.0

Sapient.AspNetCore.FlashMessages

This package provides a number of classes to provide flash message functionality (similar to that in Ruby on Rails) to ASP.NET Core Razor page applications. These messages allow for one instance of a PageModel to provide structured objects (as FlashMessage objects) to a subsequent PageModel. These messages can subsequenty be displayed on the UI, optionally using the FlashMessageTagHelper to display a Bootstrap-style alert.

Getting Started

Installing

The simplest method to install this package into your ASP.NET Core Razor Page application is to obtain using the NuGet Package Manager: Package Manager:

PM> Install-Package Sapient.AspNetCore.FlashMessages

.NET CLI

> dotnet add package Sapient.AspNetCore.FlashMessages

FlashMessages in Razor Page PageModel

To start using FlashMessages in your application, add the following using directive to your PageModel

using System;
...
using Sapient.AspNetCore.FlashMessages;

namespace FlashMessageExample
{
    public class FlashExample : PageModel
    {
        ...

        public void OnGet()
        {
            AddFlashMessage(new FlashMessage {
                MessageType = FlashMessageType.Success,
                Title = "Success!",
                Text = "The operation completed successfully.",
                Dismissible = true
            });

        }

        public IActionResult OnPost()
        {
            AddFlashMessage(new FlashMessage {
                MessageType = FlashMessageType.Danger,
                Title = "Error!",
                Text = "There was an error performing the requested action.",
                Dismissible = false
            });
            return Redirect("./AnotherPage");
        }
    }
}

Displaying FlashMessages in Razor View

From a RazorPage view, the FlashMessages can be obtained using the Get<T> extension method for the ITempDataDictionary as shown in the example below. This example will is from the bundled demonstration, and displays all of the FlashMessages as Bootstrap as a partial:

@using Sapient.AspNetCore.FlashMessages;

@if (TempData.HasFlashMessages())
{
    FlashMessage[] messages = TempData.Get<FlashMessage[]>("FlashMessages");
    @foreach (var message in messages)
    {
        <flash asp-message-type="@message.MessageType"
            asp-title="@message.Title",
            asp-message="@message.Text",
            asp-dismiss="@message.Dismissible"
            asp-bootstrap-version="3.3.7" />
    }
}

FlashMessages are stored in the TempData ITempDataDictionary in the Razor page, using the "FlashMessages" index, in a json format which is deserialized by the extension methods provided. The above example uses the FlashMessageTagHelper, which provides a custom tag to generate Bootstrap-style alerts from the FlashMessage contents.

Using the FlashMessageStore

The FlashMessageStore class provides a facility to store FlashMessages in a JSON formatted database, which can then be accessed from Razor PageModel.

Setup & Configuration of FlashMessageStore

To setup and configure the FlashModelStore, an singleton instance will usually be created for the WebHost, configured in the Startup.cs file. The example code creates an instance of a FlashMessageStore, which uses a database stored at ./FlashMessages.json as a store for FlashMessages.

Startup.cs

...
using Sapient.AspNetCore.FlashMessages;

namespace FlashMessageExample
{
    public class Startup
    {
        public IConfiguration Configuration { get; }

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public void ConfigureServices(IServiceCollection services)
        {
            FlashMessageStore flashMessageStore = new FlashMessageStore(
                "./FlashMessages.json", cacheMessages: false);
            services.AddSingleton<FlashMessageStore>(flashMessageStore);
        }

        ...
    }
}

FlashMessages.json

[
    {
        "Key": "FormError",
        "MessageType": "Danger",
        "Title": "Error!",
        "Text": "The form contains errors. Please correct and resubmit",
        "Dismissible": false
    },
    {
        "Key": "OperationSuccess",
        "MessageType": "Success",
        "Title": "Success.",
        "Text": "The requested operation completed successfully",
        "Dismissible": true
    }
]
Accessing FlashMessageStore from a Razor Page

With the FlashMessageStore registered in the startup IServiceCollection, the message store is accessed using dependency injection in the Razor PageModel. This is shown in the example code, where the IndexModel includes an instance of FlashMessageStore, which is set in the constructor.

The messages are accessed using their Key value in the database.

...
using Sapient.AspNetCore.FlashMessages;

namespace FlashMessageExample
{
    public class IndexModel : PageModel
    {
        ...

        private FlashMessageStore FlashMessages { get; set; }


        public IndexModel(FlashMessageStore flashMessageStore)
        {
            FlashMessages = flashMessageStore;
        }

        ...

        public IActionResult OnPost()
        {

            if (!ModelState.IsValid)
            {
                AddFlashMessage(this.FlashMessages["FormError"]);
                return Page();
            }
            ...
        }
    }
}

Running the tests

The project includes a number of automated tests that verify the basic functionality of some of the classes. Note that this does not yet include the PageModel & ITempDataDictionary extension methods or the FlashMessageTagHelper classes. These will be added at a later date.

To run the tests using the .NET CLI, simply enter the following command from the solution directory

> dotnet test

Contributing

This module has very limited functionality, as it was developed for a specific website. Sapient Systems will continue development, but would accept any feedback and/or improvements suggested. Please contact sapientsystems@protonmail.com to discuss your ideas, or get access to the code repository to provide your own contributions.

Authors

This module was developed by Matthew Kitchin (kitchin_ms@hotmail.com)

License

This project is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 is compatible.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.2 1,240 1/9/2018
1.0.0 1,124 1/7/2018