ASP.NET Web API 2 is designed as a stateless Web API framework, therefore, the visitor session context is not automatically created on each web request. Normally, for a Sitecore solution, it is necessary to enable Sitecore Context session-dependent features/data in some or even most of Web API calls context.
One of the options is to create Web API Route which would automatically enable session state for all Web API calls that are made via this route. To be clear, we are basically looking for enabling a regular ASP.NET session state in the context of ASP.NET Web API 2 calls.
The solution option which we consider in this specific case has the following implementation approach:
- Register a new route using ASP.NET Web API 2 – a dedicated route for the session-context-enabled Web API calls handling.
- Retrieve the new route’s object using a regular ASP.NET and enrich it with a request handler that creates a regular ASP.NET session state context.