---
title: BolContextModel
summary: API reference for BolContextModel.
status: draft
owner: SAIF IT Core Services
last_reviewed: 2026-10-12
library: BolMiddleware
reference_type: package
---

# BolContextModel

Abstract Razor `PageModel` at the base of the BOL page model chain. For each request it resolves the BOL web context, detects whether the user is external, and saves the context back after the handler runs. `BolMenuModel` and `BolPolicyModel` build on it.

## Purpose
- Resolve the web context from the form, query string, session, cookie, or a new context ([BR-BOLMIDDLEWARE-001](../../business-rules.md#br-bolmiddleware-001-web-context-resolution-precedence)).
- Save the context only when it changed or a save is forced, and only when it has content ([BR-BOLMIDDLEWARE-002](../../business-rules.md#br-bolmiddleware-002-context-save-condition), [BR-BOLMIDDLEWARE-003](../../business-rules.md#br-bolmiddleware-003-context-content-detection)).
- Maintain the short-lived `BolContext` cookie ([BR-BOLMIDDLEWARE-004](../../business-rules.md#br-bolmiddleware-004-context-cookie-lifetime-and-flags)).
- Capture external user information once per context ([BR-BOLMIDDLEWARE-005](../../business-rules.md#br-bolmiddleware-005-external-user-detection), [BR-BOLMIDDLEWARE-006](../../business-rules.md#br-bolmiddleware-006-external-user-information-lookup-once)).
- Choose the session id source ([BR-BOLMIDDLEWARE-020](../../business-rules.md#br-bolmiddleware-020-session-identifier-source-selection)).

## Context
- Namespace: `BolMiddleware.Models`
- Assembly/package: `BolMiddleware`

## Public members
| Member | Signature | Purpose |
| --- | --- | --- |
| Constructor | `BolContextModel(IBolServicesContainer services)` | Receives the services container and builds the request helper from the current request. |
| `Services` | `protected IBolServicesContainer Services` | Access to security, logging, context, menu, configuration, environment, and cookie services for derived pages. |
| `RequestHelper` | `protected IRequestHelper RequestHelper` | Reads context id, session id, and form values from the current request. |
| `MyApplicationName` | `protected string MyApplicationName` | Application name read from the required `ApplicationName` configuration value. |
| `PageName` | `string PageName { get; protected set; }` | Page identifier used to mark the current menu item. |
| `ContextValue` | `string ContextValue` | Text typed into the context textbox and posted with the form. |
| `contextCookie` | `ContextCookie contextCookie` | Deserialized `BolContext` cookie for the current request. |
| `WebContext` | `ContextHelper WebContext { get; protected set; }` | Working web context for the page: policy, claim, and related values. |
| `SaveContext` | `protected void SaveContext(bool forceSave = false)` | Persists the context through `IWebContextService` and session when it changed or `forceSave` is true. |
| `OnContextSave` | `virtual void OnContextSave()` | Hook that derived pages override to run logic before a context save. |
| `Navigate` | `protected IActionResult Navigate(NavigationOptions options)` | Saves the context and redirects to the URL built from the options with the context id appended. |
| `OnPageHandlerSelected` | `override void OnPageHandlerSelected(PageHandlerSelectedContext context)` | Initializes the context before the handler is chosen. |
| `OnPageHandlerExecuting` | `override void OnPageHandlerExecuting(PageHandlerExecutingContext context)` | Reads the environment keys, resolves the context, and captures external user information. |
| `OnPageHandlerExecuted` | `override void OnPageHandlerExecuted(PageHandlerExecutedContext context)` | Saves the context after the handler completes. |

## Behavioral notes
- Failures in context resolution degrade to an empty context instead of throwing ([BR-BOLMIDDLEWARE-021](../../business-rules.md#br-bolmiddleware-021-silent-failure-degradation)).
- The external user lookup blocks on an async call through `Task.Result`.

## Constraints and usage notes
- The cookie is deserialized without a signature; see [Security and authorization](../../security-authorization.md).
- Requires the `ApplicationName` configuration key and the two `bolmiddleware:` environment keys ([Configuration](../../configuration.md)).

## Sources
- src/BolMiddleware/Models/BolContextModel.cs
