Try our conversational search powered by Generative AI!

Redirect to old page version while Routing(Initialization)

Vote:
 

Hi All, 

I am trying to redirect to older version of same page or different page while routing. But i am getting error in "localhost redirected you too many times."

I want to load previous version of same page or different page when particular page is requsted by the user.

Code:

namespace SampleWebsite.web.Business.Initialization
{
    [InitializableModule]
    [ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
    public class RoutingExtension : IInitializableModule
    {
        private static readonly ILog Logger = LogManager.GetLogger(typeof(RoutingExtension));
        public static Injected ContentVersionRepository { get; set; }

        public void Initialize(InitializationEngine context)
        {
            //Add initialization logic, this method is called once after CMS has been initialized
            Logger.DebugFormat("Routing Initialize start");
            ContentRoute.RoutingContent += ContentRoute_RoutingContent;
            //ContentRoute.RoutedContent += ContentRoute_RoutedContent;
            Logger.DebugFormat("Routing Initialize end");
        }

        private static void ContentRoute_RoutingContent(object sender, RoutingEventArgs e)
        {
            try
            {
                IContentLoader contentLoader = ServiceLocator.Current.GetInstance();
                var urlResolver = ServiceLocator.Current.GetInstance();
                Logger.DebugFormat("ContentRoute_RoutingContent");
                var context = e.RoutingSegmentContext;
                var contentReference = UrlResolver.Current.Route(new UrlBuilder(context.RequestUrl.LocalPath)).ContentLink;

                SitePageData sampleWebsite;
                contentLoader.TryGet(contentReference, out sampleWebsite);
                
                if (sampleWebsite!= null && sampleWebsite.ExcludeFromMirroring)
                {
                    
                    var list = ContentVersionRepository.Service.List(contentReference);
                    for(int i=list.Count()-1; i>0; i--)
                    {
                        var tempContentReference=list.ElementAt(i).ContentLink;
                        contentLoader.TryGet(tempContentReference, out sampleWebsite);
                        if(!sampleWebsite.ExcludeFromMirroring)
                        {
                            Logger.DebugFormat("Routing Item");
                            e.RoutingSegmentContext.PermanentRedirect(urlResolver.GetUrl(tempContentReference));
                            //e.RoutingSegmentContext.TemporaryRedirect(urlResolver.GetUrl(tempContentReference));
                            break;
                        }
                    }
                    
                }
            }
            catch (Exception ex)
            {
                Logger.Error("ContentRoute_RoutingContent" + ex.Message.ToString());
            }
        }

        public void Preload(string[] parameters) { }

        public void Uninitialize(InitializationEngine context)
        {
            //Add uninitialization logic
        }
    }
}

Error:

Error

Thanks in Advance.

Regards,

Hari

[Pasting files is not allowed]

#179720
Edited, Jun 20, 2017 13:27
Vote:
 

Could it be so that once you redirect to previous version of page - next request ends up here as well, meaning that you do next redirect, and next etc.

Just out of curiousity - what's business requirement here?

#179890
Jun 24, 2017 7:44
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.