---
title: Resolve web context for an incoming request
summary: Resolve and populate the request's web context before the action runs.
status: draft
owner: SAIF Documentation
last_reviewed: 2026-09-17
library: SAIF.Framework.Web.Mvc
reference_type: package
operation_id: OP-01
trigger: MVC action-filter pipeline, before every action on a derived controller
---

# Resolve web context for an incoming request

Resolve and populate the request's web context before the action runs. Trigger: MVC action-filter pipeline, before every action on a derived controller.

| Field | Value |
| --- | --- |
| **Operation ID** | OP-01 |
| **Trigger** | MVC action-filter pipeline, before every action on a derived controller |
| **Inputs** | `ActionExecutingContext` (request form, query string, referrer, session), `TempData` |
| **Outputs** | `WebContext` protected property populated; `Context=` entry appended to the IIS log |
| **Primary components** | `WebContextControllerBase.OnActionExecuting(ActionExecutingContext)` |
| **Entry point** | `src/Web.Mvc/Controllers/WebContextControllerBase.cs` |

## Steps

1. Guard `filterContext` is not null (`Enforce.ArgumentNotNull`); throws on null ([BR-01](../business-rules.md#br-01-guarded-arguments-reject-null-or-empty-input)).
2. If `TempData` contains key `WebContextId`, take the context id from TempData ([BR-02](../business-rules.md#br-02-tempdata-context-id-takes-precedence)).
3. Otherwise read the context id from the request via `HttpContextExtensions.GetWebContextId` — form field `WebContextId` first, then query-string `cid`, else empty string ([BR-03](../business-rules.md#br-03-form-field-wins-over-query-string-for-context-id)).
4. If a non-blank context id was found, resolve it: `webContextService.Get(webContextId)` (delegates to [OP-04](op-04-get-web-context-by-id.md)).
5. Otherwise, if the request URL and referrer both have more than one segment and their first segments differ (arrival from a different application), resolve the referring application's context via `webContextService.Get(referrerApplicationName, Session.SessionID)` (delegates to [OP-05](op-05-get-web-context-by-application-and-session.md)) ([BR-04](../business-rules.md#br-04-cross-application-arrival-inherits-the-referring-context)).
6. Otherwise create a brand-new context: `webContextService.Get()` (delegates to [OP-03](op-03-create-a-new-web-context.md)).
7. If the request carries a non-blank `WebContextValue` form/query value, overwrite `WebContext.CurrentValue` with it ([BR-05](../business-rules.md#br-05-request-supplied-context-value-always-overrides)).
8. If `WebContext.CurrentValue` is non-blank, append `Context={value}` to the response log (`Response.AppendToLog`).
9. Call `base.OnActionExecuting(filterContext)`.

## Process flow

The following diagram visualizes this operation's control flow.

```mermaid
flowchart TD
	Start([Action filter pipeline])
	TD[Check TempData for WebContextId]
	FQ[Check form/query cid]
	Ref[Check referrer application]
	New[Create new context]
	Over[Apply WebContextValue override]
	Log[Append Context to IIS log]
	EndOk([Continue to action])
	Start --> TD
	TD --> FQ
	FQ --> Ref
	Ref --> New
	New --> Over
	TD --> Over
	FQ --> Over
	Ref --> Over
	Over --> Log
	Log --> EndOk
```

## Failure and edge-case behavior

- Null `filterContext` throws from `Enforce`.
- Failures inside the WCF-backed `webContextService` calls propagate and are subsequently caught by `HandleExceptionAttribute` ([OP-08](op-08-handle-and-log-an-unhandled-action-exception.md)).

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

## Sources
- `src/Web.Mvc/Controllers/WebContextControllerBase.cs`
