Table of Contents

Class PageStateExtensions

Namespace
Htmx.Components
Assembly
Htmx.Components.dll

Provides extension methods for accessing page state from HTTP contexts and controllers.

public static class PageStateExtensions
Inheritance
PageStateExtensions
Inherited Members

Remarks

These extensions provide convenient access to the page state service that is attached to the HTTP context by the PageStateMiddleware. Page state allows maintaining data across HTMX requests without relying on session state or hidden form fields. This is particularly useful for HTMX applications where partial page updates need to preserve component state, form data, or UI context. Unlike session state which persists across browser sessions, page state is scoped to a single page lifecycle and automatically cleaned up when the user navigates away. This provides better memory usage and avoids stale data issues while still enabling rich interactive experiences with HTMX partial updates.

Methods

GetPageState(HttpContext)

Gets the page state service from the HTTP context.

public static IPageState GetPageState(this HttpContext context)

Parameters

context HttpContext

The HTTP context to retrieve page state from.

Returns

IPageState

The page state service instance attached to the context.

Remarks

This method requires that PageStateMiddleware has been added to the application pipeline and has processed the current request. The page state is automatically attached to the HTTP context items during middleware execution.

Exceptions

InvalidOperationException

Thrown when page state is not found in the context, typically indicating that PageStateMiddleware is not registered or not properly configured.

ArgumentNullException

Thrown when context is null.

GetPageState(Controller)

Gets the page state service from a controller's HTTP context.

public static IPageState GetPageState(this Controller controller)

Parameters

controller Controller

The controller to retrieve page state from.

Returns

IPageState

The page state service instance attached to the controller's HTTP context.

Remarks

This is a convenience method that extracts the HTTP context from the controller and retrieves the page state service. It provides the same functionality as calling GetPageState(HttpContext) on the controller's HTTP context.

Exceptions

InvalidOperationException

Thrown when page state is not found in the context, typically indicating that PageStateMiddleware is not registered or not properly configured.

ArgumentNullException

Thrown when controller is null.