Skip to content

Instantly share code, notes, and snippets.

@nickwesselman
Created August 31, 2021 13:27
Show Gist options
  • Save nickwesselman/f670449840787f471ad6e4da2cc43399 to your computer and use it in GitHub Desktop.
Save nickwesselman/f670449840787f471ad6e4da2cc43399 to your computer and use it in GitHub Desktop.
Sitecore ASP.NET Core Multisite
public class SitecoreOptions
{
public static readonly string Key = "Sitecore";
public Uri InstanceUri { get; set; }
public string LayoutServicePath { get; set; } = "/sitecore/api/layout/render/jss";
public string DefaultSiteName { get; set; }
public string ApiKey { get; set; }
public Uri RenderingHostUri { get; set; }
public bool EnableExperienceEditor { get; set; }
public Uri LayoutServiceUri
{
get
{
if (InstanceUri == null) return null;
return new Uri(InstanceUri, LayoutServicePath);
}
}
/**
* ADDED TO THE GETTING STARTED EXAMPLE
* This would be populated in your appsettings.json
*/
public Dictionary<string, string> Sites { get; set; } = new Dictionary<string, string>();
}
public class Startup
{
// snip - the rest of your Startup class
public void ConfigureServices(IServiceCollection services)
{
// snip - routing, Mvc, Layout Service, etc
// Register the Sitecore Rendering Engine services.
services.AddSitecoreRenderingEngine(options =>
{
// snip - component registration
/**
* HERE'S THE IMPORTANT BITS
* Dynamically set the site name passed to the layout service based on the incoming host name.
* Logic here is simple. You may want to support wildcards, ports, etc.
*/
options.MapToRequest((httpRequest, layoutRequest) =>
{
var hostName = httpRequest.Host.Host;
var sites = Configuration.Sites;
/** Set the site name dynamically based on host name **/
layoutRequest.SiteName(sites.ContainsKey(hostName) ? sites[hostName] : Configuration.DefaultSiteName);
});
})
// snip - additional rendering engine config
// snip - other services
}
// snip - the rest of your Startup class
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment