Sitecore How-Tos

How to resolve Site from Url

In many cases we need to resolve the context site and for that we have to go through many roots to get. But there is an easy way that will give us the context site from the Url or say from the host. Use the following code to resolve the site from the url.

public string ResolveSiteFromUrl()
{
     var uri = HttpContext.Current?.Request?.Url;
     if (uri == null)
     {
          return null;
     }
     var hostNameSite = SiteContextFactory.GetSiteContext(uri.Host, "/", uri.Port);
     return hostNameSite.Name;
}

 

Leave a Reply