Example #1
0
        /// <summary>
        /// Automatically replaces the host and port in the URL with those obtained from <see cref="Settings.GetMainUrl"/>.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <returns>The URL with fixed host and port.</returns>
        public static Uri FixHost(this Uri url)
        {
            // Make sure the host is replaced only once
            string originalUrl  = url.ToString();
            string originalHost = url.GetComponents(UriComponents.HostAndPort, UriFormat.Unescaped);
            Uri    mainUrl      = Settings.GetMainUrlOrDefault(DetectCurrentWiki());
            string newHost      = mainUrl.GetComponents(UriComponents.HostAndPort, UriFormat.Unescaped);

            originalHost = CleanupPort(originalUrl, originalHost);
            newHost      = CleanupPort(mainUrl.ToString(), newHost);

            int    hostIndex = originalUrl.IndexOf(originalHost);
            string newUrl    = originalUrl.Substring(0, hostIndex) + newHost + originalUrl.Substring(hostIndex + originalHost.Length);

            return(new Uri(newUrl));
        }