public bool Execute(string url)
        {
            try
            {
                LogHelper.Info <handle404>(string.Format("NotFound url {0} (from '{1}')", url, HttpContext.Current.Request.UrlReferrer));

                // Test if the error404 not child elements
                var error404 = NotFoundHandlerHelper.GetCurrentNotFoundPageId(
                    UmbracoConfig.For.UmbracoSettings().Content.Error404Collection.ToArray(),
                    HttpContext.Current.Request.ServerVariables["SERVER_NAME"],
                    ApplicationContext.Current.Services.EntityService,
                    new PublishedContentQuery(UmbracoContext.Current.ContentCache, UmbracoContext.Current.MediaCache),
                    ApplicationContext.Current.Services.DomainService);

                if (error404.HasValue)
                {
                    _redirectID = error404.Value;
                    HttpContext.Current.Response.StatusCode = 404;
                    return(true);
                }

                return(false);
            }
            catch (Exception err)
            {
                LogHelper.Error <handle404>("An error occurred", err);
                return(false);
            }
        }
Esempio n. 2
0
    /// <summary>
    ///     Tries to find and assign an Umbraco document to a <c>PublishedRequest</c>.
    /// </summary>
    /// <param name="frequest">The <c>PublishedRequest</c>.</param>
    /// <returns>A value indicating whether an Umbraco document was found and assigned.</returns>
    public Task <bool> TryFindContent(IPublishedRequestBuilder frequest)
    {
        if (!_umbracoContextAccessor.TryGetUmbracoContext(out IUmbracoContext? umbracoContext))
        {
            return(Task.FromResult(false));
        }

        if (_logger.IsEnabled(LogLevel.Debug))
        {
            _logger.LogDebug("Looking for a page to handle 404.");
        }

        int?domainContentId = null;

        // try to find a culture as best as we can
        var errorCulture = CultureInfo.CurrentUICulture.Name;

        if (frequest.Domain != null)
        {
            errorCulture    = frequest.Domain.Culture;
            domainContentId = frequest.Domain.ContentId;
        }
        else
        {
            var route = frequest.AbsolutePathDecoded;
            var pos   = route.LastIndexOf('/');
            IPublishedContent?node = null;
            while (pos > 1)
            {
                route = route.Substring(0, pos);
                node  = umbracoContext.Content?.GetByRoute(route, culture: frequest?.Culture);
                if (node != null)
                {
                    break;
                }

                pos = route.LastIndexOf('/');
            }

            if (node != null)
            {
                Domain?d = DomainUtilities.FindWildcardDomainInPath(
                    umbracoContext.PublishedSnapshot.Domains?.GetAll(true), node.Path, null);
                if (d != null)
                {
                    errorCulture = d.Culture;
                }
            }
        }

        var error404 = NotFoundHandlerHelper.GetCurrentNotFoundPageId(
            _contentSettings.Error404Collection.ToArray(),
            _entityService,
            new PublishedContentQuery(umbracoContext.PublishedSnapshot, _variationContextAccessor, _examineManager),
            errorCulture,
            domainContentId);

        IPublishedContent?content = null;

        if (error404.HasValue)
        {
            if (_logger.IsEnabled(LogLevel.Debug))
            {
                _logger.LogDebug("Got id={ErrorNodeId}.", error404.Value);
            }

            content = umbracoContext.Content?.GetById(error404.Value);
            if (_logger.IsEnabled(LogLevel.Debug))
            {
                _logger.LogDebug(content == null
                    ? "Could not find content with that id."
                    : "Found corresponding content.");
            }
        }
        else
        {
            if (_logger.IsEnabled(LogLevel.Debug))
            {
                _logger.LogDebug("Got nothing.");
            }
        }

        frequest?
        .SetPublishedContent(content)
        .SetIs404();

        return(Task.FromResult(content != null));
    }