Normally you already specified the hostname in the site definition configuration file.
You can get the hostname by calling
if you have a list of sites domain delimited by
|, it will return you all the values from the
Then you can split by
| to get the different sites URL. If you specify the targetHostName attribute as https://www.mysite.com, then you can make use of the TargetHostName.
Proposed Solution 1
You can either change the position of the urls in the hostName from
Then when you split, you take the first one.
Proposed Solution 2
Make use of the targetHostName in your SiteDefinition.config as follows:
Then, make use of the code
var url = Sitecore.Context.Site.TargetHostName;
It is better to not use Sitecore.Context.Site.HostName but Sitecore.Context.Site.TargetHostName because the HostName property is ok while you one have only one Hostname per site in the web.config.
The TargetHostName take the property TargetHostName of the site if specified in the site tag of the web.config or the first hostname if this property is not specified.