When a single ASP.NET Core MVC application serves multiple brands, portals, or partner themes—for example, northstar vs. kestrel or a default enterprise brand with selective overrides—duplicating controllers and business logic across separate web applications quickly creates drift and maintenance drag. Equally, scattering if (brand == ...) branches throughout Razor views makes templates brittle and hard to read.
ASP.NET Core MVC solves this cleanly by combining Razor Class Libraries (RCLs) (which package precompiled Razor views, partial views, view components, and static assets into reusable assemblies) with a custom IViewLocationExpander. When resolving a view for a request, MVC calls PopulateValues(...) to include the active brand key in the view-location cache key and ExpandViewLocations(...) to probe brand-specific Razor locations before falling back to the default or shared locations.
1. Architectural Blueprint & View Resolution Flow
Plaintext
Incoming Request (e.g., X-Brand: northstar or route/claim/default)
│
▼
[BrandContextResolver] ── resolves normalized BrandKey (e.g., "northstar", "kestrel", or "default")
│ stores in HttpContext.Items
▼
[RazorViewEngine + BrandViewLocationExpander]
├─ PopulateValues(): adds "brand" -> normalized brand key to view cache key
└─ ExpandViewLocations(): searches in priority order:
1. Brand-specific view location (e.g., /Views/Brands/{brand}/{1}/{0}.cshtml)
2. Brand-specific shared view (e.g., /Views/Brands/{brand}/Shared/{0}.cshtml)
3. Standard / RCL default locations returned by base locations
Concern | Mechanism in ASP.NET Core MVC | Benefit |
Brand Cache Isolation |
| Prevents Brand A's resolved view path from poisoning Brand B's view location cache entry. |
Selective Overrides |
| Only customized views need brand-specific |
Reusable Packaging | Razor Class Libraries (RCLs) ship compiled views and assets with | Multiple MVC hosts or modular features can share brand templates cleanly without copying files. |
2. Brand Resolver, IViewLocationExpander, and Multi-Brand Views
Let's create a BrandContextResolver that resolves the active brand from a request header (X-Brand), query/route fallback, or configured default, and a BrandViewLocationExpander that injects the brand value into the Razor view cache key and expands search locations.
C#
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.Extensions.Options;
public sealed class BrandOptions
{
public const string SectionName = "Branding";
public string DefaultBrand { get; set; } = "default";
public string[] SupportedBrands { get; set; } = ["default", "northstar", "kestrel"];
}
public sealed class BrandContextResolver
{
public const string BrandHeaderName = "X-Brand";
public const string HttpContextItemKey = "__ResolvedBrandKey";
private readonly BrandOptions _options;
public BrandContextResolver(IOptions<BrandOptions> options)
{
_options = options.Value;
}
public string ResolveBrand(HttpContext context)
{
ArgumentNullException.ThrowIfNull(context);
if (context.Items.TryGetValue(HttpContextItemKey, out object? cached) &&
cached is string cachedBrand &&
!string.IsNullOrWhiteSpace(cachedBrand))
{
return cachedBrand;
}
string? candidate = null;
if (context.Request.Headers.TryGetValue(BrandHeaderName, out var headerValues) &&
!string.IsNullOrWhiteSpace(headerValues))
{
candidate = headerValues.ToString().Trim().ToLowerInvariant();
}
else if (context.Request.Query.TryGetValue("brand", out var queryValues) &&
!string.IsNullOrWhiteSpace(queryValues))
{
candidate = queryValues.ToString().Trim().ToLowerInvariant();
}
string normalizedDefault = NormalizeOrDefault(_options.DefaultBrand, "default");
string resolved = IsSupported(candidate) ? candidate! : normalizedDefault;
context.Items[HttpContextItemKey] = resolved;
return resolved;
}
private bool IsSupported(string? candidate)
{
if (string.IsNullOrWhiteSpace(candidate))
{
return false;
}
return _options.SupportedBrands.Any(b =>
string.Equals(b.Trim(), candidate, StringComparison.OrdinalIgnoreCase));
}
private static string NormalizeOrDefault(string? value, string fallback) =>
string.IsNullOrWhiteSpace(value) ? fallback : value.Trim().ToLowerInvariant();
}
public sealed class BrandViewLocationExpander : IViewLocationExpander
{
private const string BrandValueKey = "brand";
public void PopulateValues(ViewLocationExpanderContext context)
{
ArgumentNullException.ThrowIfNull(context);
var resolver = context.ActionContext.HttpContext.RequestServices
.GetRequiredService<BrandContextResolver>();
string brand = resolver.ResolveBrand(context.ActionContext.HttpContext);
context.Values[BrandValueKey] = brand;
}
public IEnumerable<string> ExpandViewLocations(
ViewLocationExpanderContext context,
IEnumerable<string> viewLocations)
{
ArgumentNullException.ThrowIfNull(context);
ArgumentNullException.ThrowIfNull(viewLocations);
string brand = context.Values.TryGetValue(BrandValueKey, out object? rawBrand) &&
rawBrand is string brandStr &&
!string.IsNullOrWhiteSpace(brandStr)
? brandStr
: "default";
// {0} = View Name, {1} = Controller Name, {2} = Area Name
string[] brandLocations =
[
$"/Views/Brands/{brand}/{{1}}/{{0}}.cshtml",
$"/Views/Brands/{brand}/Shared/{{0}}.cshtml",
$"/BrandThemes/{brand}/Views/{{1}}/{{0}}.cshtml",
$"/BrandThemes/{brand}/Views/Shared/{{0}}.cshtml"
];
foreach (string location in brandLocations)
{
yield return location;
}
foreach (string location in viewLocations)
{
yield return location;
}
}
}
Join the conversation! Your thoughts help the community grow.