Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Bo Jacobsen 597 posts 2395 karma points
    Feb 03, 2020 @ 13:26
    Bo Jacobsen
    0

    Is it possible to access the redirect manager from code?

    Hi all.

    Using Umbraco 8.5.3 and SeoChecker 2.9.0.

    I was wondering if it was possible to access all redirect urls?

    My scenario is that i use a custom 404 IContentFinder for only specific segments. i still use seoChecker redirect for these segments and SeoCheckers LastChanceFinder 404 for all other content.

    public class MyCustomContentFinder : IContentFinder
    {
        private readonly ILogger _logger;
        private readonly ISettingsService _settingsService; // Custom service
    
        public MyCustomContentFinder(ILogger logger, ISettingsService settingsService)
        {
            _logger = logger;
            _settingsService = settingsService;
        }
    
        public bool TryFindContent(PublishedRequest frequest)
        {
            try
            {
                if (frequest.UmbracoContext != null && frequest.UmbracoContext.InPreviewMode)
                {
                    return frequest.PublishedContent != null;
                }
    
                if (frequest.Uri.LocalPath.ToLower().StartsWith("/activities/"))
                {
                    /* 
                     * Here i want to find urls from the SeoChecker redirects that match frequest.Uri.LocalPath
                     * If none of the urls match i continue to next step.
                     * If a url is found i set frequest.PublishedContent to null. or simply do nothing. Depends on what the redirect can get me.
                    */
    
                    // This part works.
                    var content = frequest.UmbracoContext.Content.GetByRoute("/");
                    var notfoundDocument = _settingsService.Get404NotFoundCustomPage(content, frequest.Culture.Name);
                    if (notfoundDocument != null)
                    {
                        frequest.PublishedContent = frequest.UmbracoContext.Content.GetById(notfoundDocument.Id);
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.Error<MyCustomContentFinder>(ex, "Black magic!");
            }
    
            return frequest.PublishedContent != null;
        }
    }
    

    i tried to convert the redirect property on a document to SEOChecker.Model.RedirectManagement.RedirectOverviewItem but it is only taking the last added redirect.

  • Richard Soeteman 4035 posts 12842 karma points MVP
    Feb 04, 2020 @ 07:43
    Richard Soeteman
    100

    You can use the RedirectsRepository.Instance.GetAll() methods maybe?

  • Bo Jacobsen 597 posts 2395 karma points
    Feb 05, 2020 @ 13:03
    Bo Jacobsen
    0

    Great, that worked.

    if (frequest.Uri.LocalPath.ToLower().StartsWith("/activities/"))
    {
        var allRedirects = RedirectsRepository.Instance.GetAll(true, false);
        if(allRedirects != null && allRedirects.Any())
        {
            var tempPath = frequest.Uri.LocalPath.ToLower().Trim('/');
            var redirect = allRedirects.FirstOrDefault(x => x.Url == tempPath);
            if(redirect != null)
            {
                frequest.PublishedContent = frequest.UmbracoContext.Content.GetById(redirect.DocumentID);
            }
        }
    
        if(frequest.PublishedContent == null) 
        {
            var content = frequest.UmbracoContext.Content.GetByRoute("/");
            var notfoundDocument = _settingsService.Get404NotFoundCustomPage(content, frequest.Culture.Name);
            if (notfoundDocument != null)
            {
                frequest.PublishedContent = frequest.UmbracoContext.Content.GetById(notfoundDocument.Id);
            }
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft