Quantcast
Channel: C1 CMS Foundation - Open Source on .NET
Viewing all articles
Browse latest Browse all 2540

New Post: Multiple Hostname Bindings (Not Aliases) Going to the same Root Page (Website) in Composite C1 4.0

$
0
0
Below is what we did initially, to allow pages to resolve correctly during the routing when requested with "alternate" hostnames. We did not tackle the issue of picking the "correct" hostname when generating URLs, and we did end up abandoning this and going back to separate databases for separate environments and keeping only one hostname per website. :-)


In Composite\Plugins\Routing\Pages\DefaultPageUrlProvider.cs:
using System.Collections.Generic;
Line 236:
return ParseUrl(hostname, relativeUrl, urlSpace, out urlKind);
Line 251:
Created new private overload of ParseUrl():
private PageUrlData ParseUrl(string hostname, string relativeUrl, UrlSpace urlSpace, out UrlKind urlKind)
{
}
Made previous public overload call this private overload:
public PageUrlData ParseUrl(string relativeUrl, UrlSpace urlSpace, out UrlKind urlKind)
{
    return ParseUrl(null, relativeUrl, urlSpace, out urlKind);
}
Starting at Line 295:
            IHostnameBinding currentHostnameBinding = DataFacade.GetData<IHostnameBinding>().FirstOrDefault(b => b.Hostname == hostname);
            List<IHostnameBinding> otherHostnameBindings = null;

            if (currentHostnameBinding != null)
            {
                otherHostnameBindings = DataFacade.GetData<IHostnameBinding>().Where(b => b.HomePageId == currentHostnameBinding.HomePageId && b.Hostname != hostname)
                                                                              .ToList();
            }

            otherHostnameBindings = otherHostnameBindings ?? new List<IHostnameBinding>();

            bool firstCheck = true;
            string alternatePagePath = null;
            bool foundMatchOnAlternateHost = false;

            while (pagePath != null)
            {
                if (pageUrlBuilder.UrlToIdLookupLowerCased.TryGetValue(pagePath, out pageId))
                {
                    break;
                }
                else if (otherHostnameBindings.Any())
                {
                    foreach(IHostnameBinding hostBinding in otherHostnameBindings)
                    {
                        alternatePagePath = string.Format("http://{0}{1}", hostBinding.Hostname, pagePath).TrimEnd(new char[] { '/'});

                        if (pageUrlBuilder.UrlToIdLookupLowerCased.TryGetValue(alternatePagePath, out pageId))
                        {
                            foundMatchOnAlternateHost = true;
                            break;
                        }
                    }

                    if (foundMatchOnAlternateHost)
                    {
                        break;
                    }
                }

                if (pageUrlBuilder.RedirectUrlToIdLookupLowerCased.TryGetValue(pagePath, out pageId))
                {
                    urlKind = UrlKind.Redirect;
                    break;
                }
                else if (otherHostnameBindings.Any())
                {
                    foreach (IHostnameBinding hostBinding in otherHostnameBindings)
                    {
                        alternatePagePath = string.Format("http://{0}{1}", hostBinding.Hostname, pagePath);

                        if (pageUrlBuilder.RedirectUrlToIdLookupLowerCased.TryGetValue(alternatePagePath, out pageId))
                        {
                            foundMatchOnAlternateHost = true;
                            break;
                        }
                    }

                    if (foundMatchOnAlternateHost)
                    {
                        urlKind = UrlKind.Redirect;
                        break;
                    }
                }

            // ... rest of while loop
       }


Viewing all articles
Browse latest Browse all 2540

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>