Ejemplo n.º 1
0
        public virtual string Get404PageUrl(string requestedPath)
        {
            if (requestedPath == null)
            {
                throw new ArgumentNullException(nameof(requestedPath));
            }

            var pageUrlData = C1Helper.GetPageUrlDataFromUrl(requestedPath);
            var websiteId   = C1Helper.GetWebsiteIdFromPageUrlData(pageUrlData);

            if (pageUrlData == null || websiteId == Guid.Empty)
            {
                return(null);
            }

            var pagesConfiguration = SiteConfiguration.GetPagesConfiguration(pageUrlData.LocalizationScope, websiteId);

            if (pagesConfiguration.PageNotFoundPageId != Guid.Empty)
            {
                var pageUrl = PageService.GetPageUrl(pagesConfiguration.PageNotFoundPageId, pageUrlData.LocalizationScope);

                if (pageUrl == null)
                {
                    Log.LogWarning("PageNotFoundUrlProvider",
                                   $"404 Page is not configured for website id {pageUrlData.PageId} and culture {pageUrlData.LocalizationScope}. " +
                                   $"Requested path: {requestedPath}");

                    return(PageService.GetPageUrl(pagesConfiguration.HomePageId, pageUrlData.LocalizationScope));
                }

                var urlBuilder = new UrlBuilder(pageUrl)
                {
                    [ErrorPathQuerystringName] = HttpUtility.UrlEncode(requestedPath)
                };
                return(urlBuilder.ToString());
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 2
0
        private void OnBeginRequest(object sender, EventArgs args)
        {
            var context           = ((HttpApplication)sender).Context;
            var url               = context.Request.Url;
            var siteConfiguration = ServiceLocator.GetService <ISiteConfiguration>();
            var urlPathSegments   = url.Segments.Skip(1).Select(s => s.TrimEnd('/')).ToList(); //always skip the first segment, since it's always the first dash
            var newUrl            = string.Empty;

            try
            {
                /**
                 * Logic below identifies the type of page that is being accessed, based on the path segments that are identified, and then applies a redirect
                 * to the URL which is mapped to a page controller.
                 * For instance, a product page is identified because one of the path segments starts with "p-" and has at least 1 subsequent path (for the product id),
                 * and is then redirected to the product page with the product/variant id sent as query strings.
                 * A store page is identified with one of the segments starting with "s-" and has 1 subsequent path (for the store number).
                 *
                 * Note that this method of redirection creates a hard dependency the cutlure being explicitly passed in the URL. To account for this, we ASSUME
                 * that the culture is always passed as the first parameters in URL.
                 * This assumption is based on the ProductUrlProvider and StoreUrlProvider classes, which are responsbile for building these URLs.
                 * */

                int pathPatternIndex = -1;
                if ((pathPatternIndex = GetUrlPathIndexForSpecificPagePattern(urlPathSegments, ProductUrlPathIndicatorRegex)) > -1)
                {
                    var pageUrlData        = C1Helper.GetPageUrlDataFromUrl(url.ToString());
                    var websiteId          = siteConfiguration.GetWebsiteByPageId(pageUrlData.PageId);
                    var pagesConfiguration = siteConfiguration.GetPagesConfiguration(pageUrlData.LocalizationScope, websiteId);
                    var productPageUrl     = _pageService.GetPageUrl(pagesConfiguration.ProductPageId, pageUrlData.LocalizationScope);

                    string productId = urlPathSegments.ElementAtOrDefault(pathPatternIndex + 1); //product Id is always in the path after the product path indicator
                    string variantId = urlPathSegments.ElementAtOrDefault(pathPatternIndex + 2); //variant Id is always in the path after the product id

                    newUrl = UrlFormatter.AppendQueryString(productPageUrl, new NameValueCollection(context.Request.QueryString)
                    {
                        { "id", productId },
                        { "variantId", variantId }
                    });
                }
                else if ((pathPatternIndex = GetUrlPathIndexForSpecificPagePattern(urlPathSegments, StoreUrlPathIndicatorRegex)) > -1)
                {
                    var pageUrlData        = C1Helper.GetPageUrlDataFromUrl(url.ToString());
                    var websiteId          = siteConfiguration.GetWebsiteByPageId(pageUrlData.PageId);
                    var pagesConfiguration = siteConfiguration.GetPagesConfiguration(pageUrlData.LocalizationScope, websiteId);
                    var storePageUrl       = _pageService.GetPageUrl(pagesConfiguration.StorePageId, pageUrlData.LocalizationScope);

                    string storeNumber = urlPathSegments.ElementAtOrDefault(pathPatternIndex + 1);

                    newUrl = UrlFormatter.AppendQueryString(storePageUrl, new NameValueCollection
                    {
                        { "storeNumber", storeNumber }
                    });
                }
                else
                {
                    return;
                }
            }
            catch (Exception ex)
            {
                LoggingService.LogError("Composer Url Rewrite Module", ex);

                throw new HttpException(404, "Could not redirect page");
            }

            if (!string.IsNullOrEmpty(newUrl))
            {
                context.RewritePath(newUrl);
            }
        }