---
title: Get a web context by id (session-cache first)
summary: Return a cached web context by id, recreating it remotely on a cache miss.
status: draft
owner: SAIF Documentation
last_reviewed: 2026-09-17
library: SAIF.Framework.Web.Mvc
reference_type: package
operation_id: OP-04
trigger: OP-01 (resolve web context) step 4
---

# Get a web context by id (session-cache first)

Return a cached web context by id, recreating it remotely on a cache miss. Trigger: OP-01 (resolve web context) step 4.

| Field | Value |
| --- | --- |
| **Operation ID** | OP-04 |
| **Trigger** | OP-01 (resolve web context) step 4 |
| **Inputs** | `contextId` |
| **Outputs** | `IWebContext` |
| **Primary components** | `WebContextService.Get(string contextId)` |
| **Entry point** | `src/Web/WebContextService.cs` |

## Steps

1. `Enforce.ArgumentNotNullOrEmpty(contextId, "contextId")` — throws on null/empty ([BR-01](../business-rules.md#br-01-guarded-arguments-reject-null-or-empty-input)).
2. Look the id up in the session dictionary `items`; return the cached instance when present ([BR-10](../business-rules.md#br-10-context-lookup-is-session-cache-first)).
3. On cache miss, fall through to `CreateContext(contextId)` ([OP-03](op-03-create-a-new-web-context.md)) — a miss recreates the context remotely rather than failing.

## Process flow

The following diagram visualizes this operation's control flow.

```mermaid
flowchart TD
	Start([Get by id])
	Guard[Guard contextId not null/empty]
	Chk{In session cache?}
	Cached([Return cached instance])
	Miss[Recreate via CreateContext]
	EndOk([Return context])
	Start --> Guard
	Guard --> Chk
	Chk -->|Yes| Cached
	Chk -->|No| Miss
	Miss --> EndOk
	Cached --> EndOk
```

## Failure and edge-case behavior

- Null or empty `contextId` throws from `Enforce`.

## Related documentation
- [Operations and processes](../operations-processes.md)
- [Business rules](../business-rules.md)

## Sources
- `src/Web/WebContextService.cs`
