'Setting Scheme programmatically of website
I have a multisite application with around 40+ Https sites. The scheme column for all the sites is empty.
I tried creating a schedule job that will update Scheme to HTTPS looping in all the sites using SiteDefinitionRepository. The problem is Scheme property is read only & so I cannot set the same.
Is there a way I can set Scheme to HTTPS rather than doing it manually for the 40+sites?
Solution 1:[1]
The SiteDefinition
and HostDefinition
implementations are IReadOnly
.
Create a writable clone and set UseSecureConnection
to true in the host definition
// ISiteDefinitionRepository siteDefinitionRepository
var sites = _siteDefinitionRepository.List();
foreach (var site in sites)
{
var writableSite = site.CreateWritableClone();
if (site.SiteUrl.Scheme == "http")
{
writableSite.SiteUrl = new Uri(site.SiteUrl.ToString().Replace("http", "https"));
}
var hosts = writableSite.Hosts.Where(x => !x.Name.Equals(HostDefinition.WildcardHostName));
foreach (var writableHost in hosts)
{
writableHost.UseSecureConnection = true;
}
_siteDefinitionRepository.Save(writableSite);
}
The code will turn this
in to this
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |