1. Sitecore ASP.NET Core SDK

MultisiteMiddleware

Version: 0.x (alpha public release)

The MultisiteMiddleware in the Sitecore ASP.NET Core Rendering SDK enables routing and rendering of content based on the hostname in a multisite setup. It ensures that the correct Site context is resolved and applied for each incoming request.

Purpose

In a multisite setup, a single rendering host can serve multiple sites, each with its own content root. The MultisiteMiddleware inspects incoming requests and determines which configured site the request belongs to, based on the hostname (for example, www.sitea.com, www.siteb.com), and sets the appropriate site context for downstream processing.

Processing flow

The following outlines the workflow that governs how site context is resolved when a request is received, detailing the sequence of operations performed by the middleware.

When a request is received:

  1. The middleware checks if the SiteContext is already set.

  2. It retrieves the list of configured sites from the ISiteResolver.

  3. Site resolution follows this priority:

    • Query Parameter Override - if the query string contains sc_site, the middleware attempts to resolve the site using that value.

    • Default Resolution - if no override is present, the middleware uses the request's host and path to identify the site.

  4. If a match is found:

    • The resolved ISiteContext is stored in HttpContext.Items.

    • A log entry is written to indicate which site was resolved.

Site override with the sc_site parameter

The sc_site query parameter allows manual override of the site resolution logic. This is especially useful for:

  • Testing specific sites without modifying host headers or DNS.

  • Forcing site context during local development or previewing content.

For example, to force resolution of the site named siteb, you can use the following parameter:

https://localhost:5000/about?sc_site=siteb 
Note

The value of sc_site must match the Name field in the configured site definition and be passed on every request for the override to work.

Usage

To use this middleware, add it to your request pipeline defined in your Program.cs as shown:

app.UseSitecoreMultisite();

You can see an example of how the middleware is used to resolve site context in the XM Cloud ASP.NET Core Starter Kit.

XM Cloud is now SitecoreAI

Some code examples, images, and UI labels may still use XM Cloud while engineering assets are being updated.

Logging

The middleware logs helpful messages using the ILogger interface. This includes the following:

  • Successful resolution: Resolved site 'siteb' for host 'siteb.localhost' and path '/home'

  • Overridden resolution via sc_site: Resolved site 'siteb' using query parameter 'sc_site'

  • Failed resolution: Unable to resolve site context for request: Host='unknown.local', Path='/'

If you have suggestions for improving this article, let us know!