Created
August 31, 2021 13:27
-
-
Save nickwesselman/f670449840787f471ad6e4da2cc43399 to your computer and use it in GitHub Desktop.
Sitecore ASP.NET Core Multisite
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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