Class PageState
- Namespace
- Htmx.Components.State
- Assembly
- Htmx.Components.dll
Implements page state management with encrypted storage capabilities. The state is organized into partitions containing key-value pairs, with automatic versioning for change tracking. All data is serialized as JSON and encrypted using ASP.NET Core Data Protection.
public class PageState : IPageState
- Inheritance
-
PageState
- Implements
- Inherited Members
- Extension Methods
Constructors
PageState(IDataProtectionProvider)
Initializes a new instance of the PageState class with the specified data protection provider. Creates a data protector specifically for page state encryption and initializes with default state.
public PageState(IDataProtectionProvider dataProtectionProvider)
Parameters
dataProtectionProvider
IDataProtectionProviderThe data protection provider used to create encryption keys for state data.
Properties
Encrypted
Gets the current state as an encrypted string representation. The state is serialized to JSON and then encrypted using the data protector.
public string Encrypted { get; }
Property Value
IsDirty
Gets a value indicating whether the state has been modified since it was last loaded. This is determined by comparing the current version with the version when the state was loaded.
public bool IsDirty { get; }
Property Value
State
Gets the current state data organized as a dictionary of partitions, where each partition contains key-value pairs. The state includes a special metadata partition for internal tracking.
public Dictionary<string, Dictionary<string, string>> State { get; }
Property Value
Version
Gets the current version number of the page state. This version is incremented whenever the state is modified.
public int Version { get; }
Property Value
Methods
ClearKey(string, string)
Removes a specific key from the specified partition. If the key existed and was removed, the version is incremented.
public void ClearKey(string partition, string key)
Parameters
partition
stringThe partition name to remove the key from.
key
stringThe key to remove from the partition.
ClearPartition(string)
Removes an entire partition and all its keys from the state. If the partition existed and was removed, the version is incremented.
public void ClearPartition(string partition)
Parameters
partition
stringThe partition name to remove.
GetOrCreate<T>(string, string, Func<T>)
Retrieves a value from the specified partition and key, or creates it using the factory function if not found. If the value is created, it is stored in the state and the version is incremented.
public T GetOrCreate<T>(string partition, string key, Func<T> factory)
Parameters
partition
stringThe partition name to retrieve or store the value in.
key
stringThe key within the partition.
factory
Func<T>A function to create the value if it doesn't exist.
Returns
- T
The existing value if found, otherwise the newly created value from the factory.
Type Parameters
T
The type to deserialize/create.
Get<T>(string, string)
Retrieves a value from the specified partition and key. The value is deserialized from JSON to the specified type.
public T? Get<T>(string partition, string key)
Parameters
partition
stringThe partition name to retrieve the value from.
key
stringThe key within the partition.
Returns
- T
The deserialized value if found, otherwise the default value for type T.
Type Parameters
T
The type to deserialize the value to.
Load(string?)
Loads the page state from an encrypted string representation. If the encrypted string is null or empty, initializes with default state. Updates the internal version tracking after loading.
public void Load(string? encrypted)
Parameters
encrypted
stringThe encrypted string containing the serialized state data, or null for initial state.
Set<T>(string, string, T)
Sets a value in the specified partition and key. The value is serialized to JSON before storage, and the version is incremented.
public void Set<T>(string partition, string key, T value)
Parameters
partition
stringThe partition name to store the value in.
key
stringThe key within the partition.
value
TThe value to store.
Type Parameters
T
The type of the value to store.