• Decrease Text SizeIncrease Text Size

Executing custom code in Page base class.

Posted Date: 5/9/2023
    Printer Friendly Version   Email A Friend   Add This   Increase Text Size   Decrease Text Size
If you need to execute custom code in the Page base class during the OnPreInit event.
Copy the code create a CustomPage.cs file, and move this file into Root/App_Code/Custom/ directory


using Centralpoint.Web;
using System.Web;

namespace Centralpoint.WebSite
{
    /// <remarks>
    /// This class is used to extend the Centralpoint page.
    /// </remarks>
    public partial class Page
    {
        /// <summary>
        /// This method initializes the custom page.
        /// </summary>
        /// <param name="e">Set to the event arguments.</param>
        partial void PreInitContextItemsCustom(bool currentInfoVerifiesRole)
        {
            if (!this.IsHtmlPage) return;
            //return;
            string pageUrl = HttpContext.Current.Request.RawUrl.ToString();

            HttpContext.Current.Trace.Warn("PreInitContextItemsCustom", $"pageUrl: {pageUrl}");
            //string pattern = @"(.*\/policies\.aspx\?iid=)(BP([0-9]+)).*";
            string pattern = @"(.*\/policies\.aspx\?iid=)(BP\d{2,})";
            //string pattern = @"(.*\/policies\/(.*)\.aspx\?iid=)(BP\d{2,})";
            System.Text.RegularExpressions.Match match = System.Text.RegularExpressions.Regex.Match(pageUrl, pattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase);

            if (!match.Success)
            {
                HttpContext.Current.Trace.Warn("PreInitContextItemsCustom:!match.Success", $"{pageUrl} did not match this {pattern} pattern.");
                return;
            }

            HttpContext.Current.Trace.Warn("PreInitContextItemsCustom:match.Success", $"{pageUrl} matched this {pattern} pattern.");
            HttpContext.Current.Trace.Warn("PreInitContextItemsCustom:", $"(CurrentInfo == null) = {CurrentInfo == null}. Check if document exists.");
            if (CurrentInfo == null)
            {
                HttpContext.Current.Trace.Warn("match.Groups[0]", $"{match.Groups[0].Value}");
                //HttpContext.Current.Trace.Warn("match.Groups[1]", $"{match.Groups[1].Value}");
                //HttpContext.Current.Trace.Warn("match.Groups[2]", $"{match.Groups[2].Value}");
                //HttpContext.Current.Trace.Warn("match.Groups[3]", $"{match.Groups[3].Value}");
           
                string newUrl = $"{pageUrl.Replace("BP", "")}";
                HttpContext.Current.Trace.Warn("PreInitContextItemsCustom:", $"Redirecting to new Url: {newUrl}");
                HttpContext.Current.Response.Redirect(newUrl, true, true);
            }
        }
    }
}




Keywords: Page, Page base class, OnPreInit



Related Taxonomy

Comments:

Be the first to leave a comment.
Please Login to post comments.