Rock.Web.RockRouteHandler.IRouteHandler C# (CSharp) Method

IRouteHandler() private method

Determine the logical page being requested by evaluating the routedata, or querystring and then loading the appropriate layout (ASPX) page
private IRouteHandler ( System.Web.Routing.RequestContext requestContext ) : System.Web.IHttpHandler
requestContext System.Web.Routing.RequestContext
return System.Web.IHttpHandler
        System.Web.IHttpHandler IRouteHandler.GetHttpHandler( RequestContext requestContext )
        {
            if ( requestContext == null )
            {
                throw new ArgumentNullException( "requestContext" );
            }

            try
            {
                var siteCookie = requestContext.HttpContext.Request.Cookies["last_site"];

                string pageId = "";
                int routeId = 0;

                var parms = new Dictionary<string, string>();

                // Pages using the default routing URL will have the page id in the RouteData.Values collection
                if ( requestContext.RouteData.Values["PageId"] != null )
                {
                    pageId = (string)requestContext.RouteData.Values["PageId"];
                }

                // Pages that use a custom URL route will have the page id in the RouteDate.DataTokens collection
                else if ( requestContext.RouteData.DataTokens["PageRoutes"] != null )
                {
                    var pageAndRouteIds = requestContext.RouteData.DataTokens["PageRoutes"] as List<PageAndRouteId>;
                    if ( pageAndRouteIds != null && pageAndRouteIds.Count > 0 )
                    {
                        // Default to first site/page
                        if ( pageAndRouteIds.Count >= 1 )
                        {
                            var pageAndRouteId = pageAndRouteIds.First();
                            pageId = pageAndRouteId.PageId.ToJson();
                            routeId = pageAndRouteId.RouteId;
                        }

                        // Then check to see if any can be matched by site
                        if ( pageAndRouteIds.Count > 1 )
                        {
                            SiteCache site = SiteCache.GetSiteByDomain( requestContext.HttpContext.Request.Url.Host );
                            if ( site == null )
                            {
                                // Use last site
                                if ( siteCookie != null && siteCookie.Value != null )
                                {
                                    site = SiteCache.Read( siteCookie.Value.AsInteger() );
                                }
                            }

                            if ( site != null )
                            {
                                foreach ( var pageAndRouteId in pageAndRouteIds )
                                {
                                    var pageCache = PageCache.Read( pageAndRouteId.PageId );
                                    if ( pageCache != null && pageCache.Layout != null && pageCache.Layout.SiteId == site.Id )
                                    {
                                        pageId = pageAndRouteId.PageId.ToJson();
                                        routeId = pageAndRouteId.RouteId;
                                        break;
                                    }
                                }
                            }
                        }
                    }

                    foreach ( var routeParm in requestContext.RouteData.Values )
                    {
                        parms.Add( routeParm.Key, (string)routeParm.Value );
                    }
                }

                // If page has not been specified get the site by the domain and use the site's default page
                if ( string.IsNullOrEmpty( pageId ) )
                {
                    SiteCache site = SiteCache.GetSiteByDomain( requestContext.HttpContext.Request.Url.Host );
                    if ( site == null )
                    {
                        // Use last site
                        if ( siteCookie != null && siteCookie.Value != null )
                        {
                            site = SiteCache.Read( siteCookie.Value.AsInteger() );
                        }
                    }

                    // if not found use the default site
                    if ( site == null )
                    {
                        site = SiteCache.Read( SystemGuid.Site.SITE_ROCK_INTERNAL.AsGuid() );
                    }

                    if ( site != null )
                    {
                        // If site has has been enabled for mobile redirect, then we'll need to check what type of device is being used
                        if ( site.EnableMobileRedirect )
                        {
                            bool redirect = false;

                            // get the device type
                            string u = requestContext.HttpContext.Request.UserAgent;

                            var clientType = PageViewUserAgent.GetClientType( u );

                            // first check if device is a mobile device
                            if ( clientType == "Mobile" )
                            {
                                redirect = true;
                            }

                            // if not, mobile device and tables should be redirected also, check if device is a tablet
                            if ( !redirect && site.RedirectTablets )
                            {
                                if ( clientType == "Tablet" )
                                {
                                    redirect = true;
                                }
                            }

                            if ( redirect )
                            {
                                if ( site.MobilePageId.HasValue )
                                {
                                    pageId = site.MobilePageId.Value.ToString();
                                }
                                else if ( !string.IsNullOrWhiteSpace( site.ExternalUrl ) )
                                {
                                    requestContext.HttpContext.Response.Redirect( site.ExternalUrl );
                                    return null;
                                }
                            }
                        }

                        if ( string.IsNullOrWhiteSpace( pageId ) )
                        {
                            if ( site.DefaultPageId.HasValue )
                            {
                                pageId = site.DefaultPageId.Value.ToString();
                            }

                            if ( site.DefaultPageRouteId.HasValue )
                            {
                                routeId = site.DefaultPageRouteId.Value;
                            }
                        }
                    }

                    if ( string.IsNullOrEmpty( pageId ) )
                    {
                        throw new SystemException( "Invalid Site Configuration" );
                    }
                }

                PageCache page = null;

                if ( !string.IsNullOrEmpty( pageId ) )
                {
                    int pageIdNumber = 0;
                    if ( Int32.TryParse( pageId, out pageIdNumber ) )
                    {
                        page = PageCache.Read( pageIdNumber );
                    }
                }

                if ( page == null )
                {
                    // try to get site's 404 page
                    SiteCache site = SiteCache.GetSiteByDomain( requestContext.HttpContext.Request.Url.Host );
                    if ( site == null )
                    {
                        // Use last site
                        if ( siteCookie != null && siteCookie.Value != null )
                        {
                            site = SiteCache.Read( siteCookie.Value.AsInteger() );
                        }
                    }

                    if ( site != null && site.PageNotFoundPageId != null )
                    {
                        if ( Convert.ToBoolean( GlobalAttributesCache.Read().GetValue( "Log404AsException" ) ) )
                        {
                            Rock.Model.ExceptionLogService.LogException(
                                new Exception( string.Format( "404 Error: {0}", requestContext.HttpContext.Request.Url.AbsoluteUri ) ),
                                requestContext.HttpContext.ApplicationInstance.Context );
                        }

                        page = PageCache.Read( site.PageNotFoundPageId ?? 0 );
                    }
                    else
                    {
                        // no 404 page found for the site, return the default 404 error page
                        return (System.Web.UI.Page)BuildManager.CreateInstanceFromVirtualPath( "~/Http404Error.aspx", typeof( System.Web.UI.Page ) );
                    }

                }

                string theme = page.Layout.Site.Theme;
                string layout = page.Layout.FileName;
                string layoutPath = PageCache.FormatPath( theme, layout );

                if ( siteCookie == null )
                {
                    siteCookie = new System.Web.HttpCookie( "last_site", page.Layout.SiteId.ToString() );
                }
                else
                {
                    siteCookie.Value = page.Layout.SiteId.ToString();
                }
                requestContext.HttpContext.Response.SetCookie( siteCookie );

                try
                {
                    // Return the page for the selected theme and layout
                    Rock.Web.UI.RockPage cmsPage = (Rock.Web.UI.RockPage)BuildManager.CreateInstanceFromVirtualPath( layoutPath, typeof( Rock.Web.UI.RockPage ) );
                    cmsPage.SetPage( page );
                    cmsPage.PageReference = new PageReference( page.Id, routeId, parms, requestContext.HttpContext.Request.QueryString );
                    return cmsPage;
                }
                catch ( System.Web.HttpException )
                {
                    // The Selected theme and/or layout didn't exist, attempt first to use the layout in the default theme.
                    theme = "Rock";

                    // If not using the default layout, verify that Layout exists in the default theme directory
                    if ( layout != "FullWidth" &&
                        !File.Exists( requestContext.HttpContext.Server.MapPath( string.Format( "~/Themes/Rock/Layouts/{0}.aspx", layout ) ) ) )
                    {
                        // If selected layout doesn't exist in the default theme, switch to the Default layout
                        layout = "FullWidth";
                    }

                    // Build the path to the aspx file to
                    layoutPath = PageCache.FormatPath( theme, layout );

                    // Return the default layout and/or theme
                    Rock.Web.UI.RockPage cmsPage = (Rock.Web.UI.RockPage)BuildManager.CreateInstanceFromVirtualPath( layoutPath, typeof( Rock.Web.UI.RockPage ) );
                    cmsPage.SetPage( page );
                    cmsPage.PageReference = new PageReference( page.Id, routeId, parms, requestContext.HttpContext.Request.QueryString );
                    return cmsPage;
                }
            }
            catch (Exception ex)
            {
                if ( requestContext.HttpContext != null )
                {
                    requestContext.HttpContext.Cache["RockExceptionOrder"] = "66";
                    requestContext.HttpContext.Cache["RockLastException"] = ex;
                }

                System.Web.UI.Page errorPage = (System.Web.UI.Page)BuildManager.CreateInstanceFromVirtualPath( "~/Error.aspx", typeof( System.Web.UI.Page ) );
                return errorPage;
            }
        }
RockRouteHandler