Sitecore Conditional Rendering based on Host name.

Hi Folks,

Requirement :

Single website should behave as two different website, and show or hide rendering conditionally based on host name. It’s a small solution but can prove very useful.

Solution:

Host Name Condition

using Sitecore.Diagnostics;
using Sitecore.Rules;
using Sitecore.Rules.Conditions;
using System.Web;

namespace Framework.Extension.Sitecore.Rules.Conditions
{
	public class HasHostNameCondition<T> : StringOperatorCondition<T> where T : RuleContext
	{
		public string Value
		{
			get;
			set;
		}
		protected override bool Execute(T ruleContext)
		{

			if (HttpContext.Current != null && !string.IsNullOrEmpty(HttpContext.Current.Request.Url.Host.ToString()))
			{
				return base.Compare(HttpContext.Current.Request.Url.Host.ToString().ToLower(), this.Value.ToLower());
			}
			return false;
		}
	}
}

 

Thanks for reading.
Mrunal