Esempio n. 1
0
    /// <summary>
    ///     Special check for when no template or hijacked route is done which needs to re-run through the routing pipeline
    ///     again for last chance finders
    /// </summary>
    private async Task <UmbracoRouteValues> CheckNoTemplateAsync(
        HttpContext httpContext, UmbracoRouteValues def, bool hasHijackedRoute)
    {
        IPublishedRequest request = def.PublishedRequest;

        // Here we need to check if there is no hijacked route and no template assigned but there is a content item.
        // If this is the case we want to return a blank page.
        // We also check if templates have been disabled since if they are then we're allowed to render even though there's no template,
        // for example for json rendering in headless.
        if (request.HasPublishedContent() &&
            !request.HasTemplate() &&
            !_umbracoFeatures.Disabled.DisableTemplates &&
            !hasHijackedRoute)
        {
            IPublishedContent?content = request.PublishedContent;

            // This is basically a 404 even if there is content found.
            // We then need to re-run this through the pipeline for the last
            // chance finders to work.
            // Set to null since we are telling it there is no content.
            request = await _publishedRouter.UpdateRequestAsync(request, null);

            if (request == null)
            {
                throw new InvalidOperationException(
                          $"The call to {nameof(IPublishedRouter.UpdateRequestAsync)} cannot return null");
            }

            string?customActionName = GetTemplateName(request);

            def = new UmbracoRouteValues(
                request,
                def.ControllerActionDescriptor,
                customActionName);

            // if the content has changed, we must then again check for hijacked routes
            if (content != request.PublishedContent)
            {
                def = CheckHijackedRoute(httpContext, def, out _);
            }
        }

        return(def);
    }
    private async Task <UmbracoRouteValues> SetPublishedContentAsOtherPageAsync(
        HttpContext httpContext, IPublishedRequest?publishedRequest, int pageId)
    {
        if (pageId != publishedRequest?.PublishedContent?.Id)
        {
            IUmbracoContext   umbracoContext   = _umbracoContextAccessor.GetRequiredUmbracoContext();
            IPublishedContent?publishedContent = umbracoContext.PublishedSnapshot.Content?.GetById(pageId);
            if (publishedContent is null || publishedRequest is null)
            {
                throw new InvalidOperationException("No content found by id " + pageId);
            }

            IPublishedRequest reRouted = await _publishedRouter.UpdateRequestAsync(publishedRequest, publishedContent);

            // we need to change the content item that is getting rendered so we have to re-create UmbracoRouteValues.
            UmbracoRouteValues updatedRouteValues = await _umbracoRouteValuesFactory.CreateAsync(httpContext, reRouted);

            return(updatedRouteValues);
        }

        throw new InvalidOperationException(
                  "Public Access rule has a redirect node set to itself, nothing can be routed.");
    }