Setting Scheme programmatically of website

110 Views Asked by At

I have a multisite application with around 40+ Https sites. The scheme column for all the sites is empty.

enter image description here 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.

enter image description here

Is there a way I can set Scheme to HTTPS rather than doing it manually for the 40+sites?

1

There are 1 best solutions below

1
On BEST ANSWER

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

enter image description here

in to this

enter image description here