Beispiel #1
0
 /// <summary>
 /// Creates the rock page for the selected theme and layout.
 /// </summary>
 /// <param name="page">The page.</param>
 /// <param name="layoutPath">The layout path.</param>
 /// <param name="routeHttpRequest">The routeHttpRequest.</param>
 /// <param name="parms">The parms.</param>
 /// /// <param name="routeId">The routeId.</param>
 /// <returns></returns>
 private Rock.Web.UI.RockPage CreateRockPage(PageCache page, string layoutPath, int routeId, Dictionary <string, string> parms, HttpRequestBase routeHttpRequest)
 {
     // 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, routeHttpRequest.QueryString);
     return(cmsPage);
 }
Beispiel #2
0
        /// <summary>
        /// Determine the logical page being requested by evaluating the routedata, or querystring and
        /// then loading the appropriate layout (ASPX) page
        /// </summary>
        /// <param name="requestContext"></param>
        /// <returns></returns>
        System.Web.IHttpHandler IRouteHandler.GetHttpHandler(RequestContext requestContext)
        {
            if (requestContext == null)
            {
                throw new ArgumentNullException("requestContext");
            }

            try
            {
                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["PageId"] != null)
                {
                    pageId  = (string)requestContext.RouteData.DataTokens["PageId"];
                    routeId = Int32.Parse((string)requestContext.RouteData.DataTokens["RouteId"]);

                    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
                else
                {
                    SiteCache site = SiteCache.GetSiteByDomain(requestContext.HttpContext.Request.Url.Host);

                    // 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 && 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);

                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);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Determine the logical page being requested by evaluating the routedata, or querystring and
        /// then loading the appropriate layout (ASPX) page
        /// </summary>
        /// <param name="requestContext"></param>
        /// <returns></returns>
        System.Web.IHttpHandler IRouteHandler.GetHttpHandler(RequestContext requestContext)
        {
            if (requestContext == null)
            {
                throw new ArgumentNullException("requestContext");
            }

            try
            {
                var httpRequest = requestContext.HttpContext.Request;

                var siteCookie = httpRequest.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 = null;

                            // First check to see if site was specified in querystring
                            int?siteId = httpRequest.QueryString["SiteId"].AsIntegerOrNull();
                            if (siteId.HasValue)
                            {
                                site = SiteCache.Read(siteId.Value);
                            }

                            // Then check to see if site can be determined by domain
                            if (site == null)
                            {
                                site = SiteCache.GetSiteByDomain(httpRequest.Url.Host);
                            }

                            // Then check the last site
                            if (site == null)
                            {
                                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(httpRequest.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)
                    {
                        // Check to see if this is a short link route
                        if (requestContext.RouteData.Values.ContainsKey("shortlink"))
                        {
                            string shortlink = requestContext.RouteData.Values["shortlink"].ToString();
                            using (var rockContext = new Rock.Data.RockContext())
                            {
                                var pageShortLink = new PageShortLinkService(rockContext).GetByToken(shortlink, site.Id);
                                if (pageShortLink != null)
                                {
                                    string trimmedUrl = pageShortLink.Url.RemoveCrLf().Trim();

                                    var transaction = new ShortLinkTransaction();
                                    transaction.PageShortLinkId = pageShortLink.Id;
                                    transaction.Token           = pageShortLink.Token;
                                    transaction.Url             = trimmedUrl;
                                    if (requestContext.HttpContext.User != null)
                                    {
                                        transaction.UserName = requestContext.HttpContext.User.Identity.Name;
                                    }
                                    transaction.DateViewed = RockDateTime.Now;
                                    transaction.IPAddress  = UI.RockPage.GetClientIpAddress(httpRequest);
                                    transaction.UserAgent  = httpRequest.UserAgent ?? "";
                                    RockQueue.TransactionQueue.Enqueue(transaction);

                                    requestContext.HttpContext.Response.Redirect(trimmedUrl);
                                    return(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)
                        {
                            // get the device type
                            string u = httpRequest.UserAgent;

                            var clientType = InteractionDeviceType.GetClientType(u);

                            bool redirect = false;

                            // 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(httpRequest.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}", httpRequest.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, httpRequest.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, httpRequest.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);
            }
        }
Beispiel #4
0
        /// <summary>
        /// Determine the logical page being requested by evaluating the routedata, or querystring and
        /// then loading the appropriate layout (ASPX) page
        /// </summary>
        /// <param name="requestContext"></param>
        /// <returns></returns>
        System.Web.IHttpHandler IRouteHandler.GetHttpHandler(RequestContext requestContext)
        {
            if (requestContext == null)
            {
                throw new ArgumentNullException("requestContext");
            }

            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["PageId"] != null)
            {
                pageId  = (string)requestContext.RouteData.DataTokens["PageId"];
                routeId = Int32.Parse((string)requestContext.RouteData.DataTokens["RouteId"]);

                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
            else
            {
                SiteCache site = SiteCache.GetSiteByDomain(requestContext.HttpContext.Request.Url.Host);

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

                if (site != null)
                {
                    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 && site.PageNotFoundPageId != null)
                {
                    page = PageCache.Read(site.PageNotFoundPageId ?? 0);
                }
                else
                {
                    // no 404 page found for the site
                    return(new HttpHandlerError(404));
                }
            }

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

            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 != "Default" &&
                    !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 = "Default";
                }

                // 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);
            }
        }
        // Handlers

        private System.Web.IHttpHandler GetHandlerForPage(RequestContext requestContext, PageCache page, int routeId = 0, bool checkMobile = true)
        {
            var site = page.Layout.Site;

            // Check for a mobile redirect on the site
            if (checkMobile && site.EnableMobileRedirect)
            {
                var clientType = InteractionDeviceType.GetClientType(requestContext.HttpContext.Request.UserAgent);

                if (clientType == "Mobile" || (site.RedirectTablets && clientType == "Tablet"))
                {
                    if (site.MobilePageId.HasValue)
                    {
                        var mobilePage = PageCache.Get(site.MobilePageId.Value);
                        if (mobilePage != null)
                        {
                            return(GetHandlerForPage(requestContext, mobilePage, routeId, false));
                        }
                    }
                    else if (!string.IsNullOrWhiteSpace(site.ExternalUrl))
                    {
                        requestContext.HttpContext.Response.Redirect(site.ExternalUrl);
                        return(null);
                    }
                }
            }

            // Set the last site cookie
            var siteCookie = requestContext.HttpContext.Request.Cookies["last_site"];

            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);

            // Get the Layout & Theme Details
            string theme      = page.Layout.Site.Theme;
            string layout     = page.Layout.FileName;
            string layoutPath = PageCache.FormatPath(theme, layout);

            // Get any route parameters
            var parms = new Dictionary <string, string>();

            foreach (var routeParm in requestContext.RouteData.Values)
            {
                if (routeParm.Key != "PageId")
                {
                    parms.Add(routeParm.Key, (string)routeParm.Value);
                }
            }

            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);
            }
        }
Beispiel #6
0
        /// <summary>
        /// Determine the logical page being requested by evaluating the routedata, or querystring and
        /// then loading the appropriate layout (ASPX) page
        /// </summary>
        /// <param name="requestContext"></param>
        /// <returns></returns>
        System.Web.IHttpHandler IRouteHandler.GetHttpHandler(RequestContext requestContext)
        {
            if (requestContext == null)
            {
                throw new ArgumentNullException("requestContext");
            }

            string pageId  = "";
            int    routeId = -1;

            // 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["PageId"] != null)
            {
                pageId  = (string)requestContext.RouteData.DataTokens["PageId"];
                routeId = Int32.Parse((string)requestContext.RouteData.DataTokens["RouteId"]);
            }
            // If page has not been specified get the site by the domain and use the site's default page
            else
            {
                string host     = requestContext.HttpContext.Request.Url.Host;
                string cacheKey = "Rock:DomainSites";

                ObjectCache cache = MemoryCache.Default;
                Dictionary <string, int> sites = cache[cacheKey] as Dictionary <string, int>;
                if (sites == null)
                {
                    sites = new Dictionary <string, int>();
                }

                Rock.Web.Cache.SiteCache site = null;
                if (sites.ContainsKey(host))
                {
                    site = Rock.Web.Cache.SiteCache.Read(sites[host]);
                }
                else
                {
                    int siteId = 1;

                    Rock.Model.SiteDomainService siteDomainService = new Rock.Model.SiteDomainService();
                    Rock.Model.SiteDomain        siteDomain        = siteDomainService.GetByDomainContained(requestContext.HttpContext.Request.Url.Host);
                    if (siteDomain != null)
                    {
                        siteId = siteDomain.SiteId;
                    }
                    else
                    {
                        var siteService = new Rock.Model.SiteService();
                        var rockSite    = siteService.Get(SystemGuid.Site.SITE_ROCK_CHMS);
                        if (rockSite != null)
                        {
                            siteId = rockSite.Id;
                        }
                    }

                    sites.Add(host, siteId);
                    site = Rock.Web.Cache.SiteCache.Read(siteId);
                }

                cache[cacheKey] = sites;

                if (site != null && site.DefaultPageId.HasValue)
                {
                    pageId = site.DefaultPageId.Value.ToString();
                }

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

            Rock.Web.Cache.PageCache page = null;

            if (!string.IsNullOrEmpty(pageId))
            {
                page = Rock.Web.Cache.PageCache.Read(Convert.ToInt32(pageId));
                if (page == null)
                {
                    return(new HttpHandlerError(404));
                }
            }

            if (page != null && !String.IsNullOrEmpty(page.LayoutPath))
            {
                // load the route id
                page.RouteId = routeId;

                // Return the page using the cached route
                Rock.Web.UI.RockPage cmsPage = (Rock.Web.UI.RockPage)BuildManager.CreateInstanceFromVirtualPath(page.LayoutPath, typeof(Rock.Web.UI.RockPage));
                cmsPage.CurrentPage = page;
                return(cmsPage);
            }
            else
            {
                string theme      = "RockCms";
                string layout     = "Default";
                string layoutPath = Rock.Web.Cache.PageCache.FormatPath(theme, layout);

                if (page != null)
                {
                    // load the route id
                    page.RouteId = routeId;

                    theme      = page.Site.Theme;
                    layout     = page.Layout;
                    layoutPath = Rock.Web.Cache.PageCache.FormatPath(theme, layout);

                    page.LayoutPath = layoutPath;
                }
                else
                {
                    page = Cache.PageCache.Read(new Model.Page());
                }

                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.CurrentPage = page;
                    return(cmsPage);
                }
                catch (System.Web.HttpException)
                {
                    // The Selected theme and/or layout didn't exist, attempt first to use the default layout in the selected theme
                    layout = "Default";

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

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

                    if (page != null)
                    {
                        page.LayoutPath = layoutPath;
                    }

                    // 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.CurrentPage = page;
                    return(cmsPage);
                }
            }
        }