using System; using CMS.CMSHelper; using CMS.GlobalHelper; using CMS.PortalControls; using CMS.PortalEngine; public partial class CMSWebParts_General_RandomRedirection : CMSAbstractWebPart { #region Webpart properties /// /// List of URLs to where webpart could redirect. /// public string RedirectionURLs { get { return ValidationHelper.GetString(GetValue("RedirectionURLs"), ""); } set { SetValue("RedirectionURLs", value); } } #endregion #region Webpart methods /// /// Content loaded event handler. /// public override void OnContentLoaded() { base.OnContentLoaded(); SetupControl(); } /// /// Initializes the control properties. /// protected void SetupControl() { if (!StopProcessing) { if ((RedirectionURLs.Trim().Length > 0) && (CMSContext.ViewMode == ViewModeEnum.LiveSite)) { // Parse URLs string string[] URLs = RedirectionURLs.Trim().Split(new String[] { "\n" }, StringSplitOptions.RemoveEmptyEntries); if (URLs.Length > 0) { // Generate random integer index to array int rndID = new Random().Next(0, URLs.Length); string newURL = URLHelper.ResolveUrl(URLs[rndID].Trim()); if ((URLHelper.CurrentURL != newURL) && (URLHelper.GetAbsoluteUrl(URLHelper.CurrentURL) != newURL)) { URLHelper.ResponseRedirect(newURL); } } } } } /// /// Reloads the control data. /// public override void ReloadData() { base.ReloadData(); SetupControl(); } #endregion }