---
title: Save web context
summary: Persists the context to the cookie, the remote BOL Context API, and the session.
status: draft
owner: SAIF IT Core Services
last_reviewed: 2026-10-12
library: BolMiddleware
reference_type: package
operation_id: OP-06
trigger: SaveContext call from a page or navigation
---

# Save web context

Consumer pages call `SaveContext` after changing the context, and navigation calls it before leaving an application. The operation keeps the three context stores aligned while avoiding a remote call on every request.

| Field | Value |
| --- | --- |
| **Operation ID** | OP-06 |
| **Trigger** | `SaveContext` call from a page or navigation |
| **Inputs** | `forceSave` flag, current `WebContext` |
| **Outputs** | Updated `BolContext` cookie, session copy, and optionally the remote record |
| **Primary components** | `BolContextModel.SaveContext`, `WebContextService.SaveContext`, `BolServiceSessionExtensions.SaveWebContext` |
| **Entry point** | `src/BolMiddleware/Models/BolContextModel.cs` |

## Steps

1. Call the virtual `OnContextSave` hook so the consumer can add items to the context before it is written.
2. If the context has content, write it to the `BolContext` cookie with a ten second lifetime ([BR-BOLMIDDLEWARE-003](../business-rules.md#br-bolmiddleware-003-context-content-detection), [BR-BOLMIDDLEWARE-004](../business-rules.md#br-bolmiddleware-004-context-cookie-lifetime-and-flags)).
3. If `forceSave` is true or the target application differs from the current application, call `WebContextService.SaveContext` to persist through the BOL Context API ([BR-BOLMIDDLEWARE-002](../business-rules.md#br-bolmiddleware-002-context-save-condition)).
   - On API failure: the service logs the error and returns false.
4. Always save the context to the session through `SaveWebContext`, which also stores the context id.

## Process flow

The following diagram visualizes this operation's control flow.

```mermaid
flowchart TD
	Start([SaveContext]) --> S1[OnContextSave hook]
	S1 --> D1{Has content?}
	D1 -->|Yes| S2[Write BolContext cookie]
	D1 -->|No| D2
	S2 --> D2{forceSave or different application?}
	D2 -->|Yes| S3[Save through BOL Context API]
	D2 -->|No| S4
	S3 --> S4[Save to session]
	S4 --> EndOk([Done])
```

## Failure and edge-case behavior

- A failed API save does not stop the cookie or session writes and does not surface to the page ([BR-BOLMIDDLEWARE-021](../business-rules.md#br-bolmiddleware-021-silent-failure-degradation)).
- The cookie has no Secure or SameSite attribute and its expiry uses the local server time.

## Related documentation
- [Index](../index.md)
- [Overview](../overview.md)
- [Architecture](../architecture.md)
- [Dependencies](../dependencies.md)
- [Configuration](../configuration.md)
- [Operations and processes](../operations-processes.md)
- [Business rules](../business-rules.md)
- [Security and authorization](../security-authorization.md)
- [Service registration](../service-registration.md)
- [API reference](../api/index.md)

## Sources
- src/BolMiddleware/Models/BolContextModel.cs
- src/BolMiddleware/Services/Context/WebContextService.cs
- src/BolMiddleware/Extensions/BolMiddlewareExtensions.cs
- src/BolMiddleware/Services/CookieService/CookieService.cs
