Copied to clipboard

Flag this post as spam?

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


  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Jan 23, 2014 @ 13:50
    Ali Sheikh Taheri
    0

    redirect all nodes with no template to ancestor with a template

    Hi

    I am working on an Umbraco project version 6.1.6 and I would like to redirect all the nodes with no template to ancestor page with a template.

    I have tried to do it with IContentFinder but didn't have any luck.

    Any suggestion or has anyone done this before?

    here is my code by using IContentFinder

    using Umbraco.Core.Models;
    using Umbraco.Web.Routing;
    
        class MyClass : IContentFinder
        {
            public bool TryFindContent(PublishedContentRequest contentRequest)
            {
                if (contentRequest != null && contentRequest.PublishedContent != null)
                {
                    if (contentRequest.PublishedContent.TemplateId == 0)
                    {
                        contentRequest.PublishedContent = GetPageWithTemplate(contentRequest.PublishedContent);
                    }
                }
    
                return contentRequest.PublishedContent != null;
            }
    
            private IPublishedContent GetPageWithTemplate(IPublishedContent publishedContent)
            {
                if (publishedContent == null)
                {
                    return null;
                }
    
                if (publishedContent.TemplateId != 0)
                {
                    return publishedContent;
                }
    
                return GetPageWithTemplate(publishedContent.Parent);
            }
        }
    
  • Lee Kelleher 4024 posts 15833 karma points MVP 13x admin c-trib
    Jan 24, 2014 @ 17:55
    Lee Kelleher
    101

    Hi Ali,

    With your IContentFinder, I'm not sure how you are inserting it into the ContentFinderResolver. It possibly isn't hitting it as Umbraco might have already found the content node?

    An alternative way could be to hook into the PublishedContentRequest Prepared event...

    Umbraco.Web.Routing.PublishedContentRequest.Prepared += PublishedContentRequest_Prepared;
    

    Then check if the node has a template associated with it...

    private void PublishedContentRequest_Prepared(object sender, System.EventArgs e)
    {
        var request = sender as Umbraco.Web.Routing.PublishedContentRequest;
        if (request == null || !request.HasPublishedContent)
            return;
    
        if (!request.HasTemplate)
        {
            var ancestorWithTemplate = request
                .PublishedContent
                .Ancestors()
                .FirstOrDefault(x => x.TemplateId != 0);
    
            UmbracoContext.Current.HttpContext.Response.Redirect(ancestorWithTemplate.Url);
        }
    }
    

    With the last line, I wasn't sure whether you wanted to redirect to the ancestor's page/URL, or if you wanted to keep the same URL and load in different content? If so, you could do this instead of the redirect.

    request.PublishedContent = ancestorWithTemplate;
    

    Good luck!

    Cheers,
    - Lee

  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Jan 24, 2014 @ 18:41
    Ali Sheikh Taheri
    0

    Hi Lee,

    Thank you so much for your quick reply.

    The debugger doesn't seem to hit this code at all.

    I've added the event to started and starting event but no luck, Am I missing anything?

    Just to make sure that we are on the same page, let explain the situation a little bit lets say I've got /Blog/2013/MyPost as url.

    Basically what I would like to do is because the year node doesn't have any template I want to redirect all the year nodes (and any other node without template) to their parent.

    Many Thanks

    Ali

  • Lee Kelleher 4024 posts 15833 karma points MVP 13x admin c-trib
    Jan 24, 2014 @ 18:44
    Lee Kelleher
    1

    I added the event to Umbraco.Core.ApplicationEventHandler.ApplicationStarted.

  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Jan 24, 2014 @ 19:18
    Ali Sheikh Taheri
    0

    I've added that to ApplicationStarted too, I am using umbraco 6.1.6 and still no luck :(

    here is my code. (I've also changed the order of the base to be executed first but no luck )

    using System.Linq;
    
        using Umbraco.Core;
        using Umbraco.Web;
    
        class myclass : ApplicationEventHandler
        {
    
            protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                Umbraco.Web.Routing.PublishedContentRequest.Prepared += PublishedContentRequest_Prepared; 
                base.ApplicationStarted(umbracoApplication, applicationContext);
            }
    
            private void PublishedContentRequest_Prepared(object sender, System.EventArgs e)
            {
                var request = sender as Umbraco.Web.Routing.PublishedContentRequest;
                if (request == null || !request.HasPublishedContent)
                    return;
    
                if (!request.HasTemplate)
                {
                    var ancestorWithTemplate = request
                        .PublishedContent
                        .Ancestors()
                        .FirstOrDefault(x => x.TemplateId != 0);
    
                    UmbracoContext.Current.HttpContext.Response.Redirect(ancestorWithTemplate.Url);
                }
            }
        }
    

    Many Thanks

    Ali

  • Lee Kelleher 4024 posts 15833 karma points MVP 13x admin c-trib
    Jan 24, 2014 @ 19:29
    Lee Kelleher
    1

    Hmmm... is it that it's not running at all, or that the debug breakpoints are hitting?

    (If it's the breakpoints, do double-check that you have "debug=true" - just covering my bases)

    Otherwise, I'm not sure why it isn't running.

  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Jan 24, 2014 @ 19:52
    Ali Sheikh Taheri
    0

    Actually It is not hitting the breakpoint and not running at all :(

    anyway thank you for your help and have a nice weekend.

    Cheers

    Ali

  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Feb 04, 2014 @ 19:00
    Andy Butland
    2

    Ali - how about making your class public?  If you have it in a seperate assembly from your start-up handler, it may not be found.

  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Feb 04, 2014 @ 19:12
    Ali Sheikh Taheri
    0

    Thank you so much Andy that was the problem. I had that in a separate project.

    Lee thank you so much for your solution.

    to be honest I would like to mark both comments from Andy & Lee as answer but unfortunately I have only one option. :(

  • Stephen 767 posts 2273 karma points c-trib
    Feb 05, 2014 @ 08:34
    Stephen
    2

    Glad you found a solution. On thing worth noting: try to avoid doing

    UmbracoContext.Current.HttpContext.Response.Redirect(ancestorWithTemplate.Url);

    In your code. Much better to let Umbraco take care of it:

    request.SetRedirect(ancestorWithTemplate.Url)

    or prob in your case you want it permanent;

    request.SetRedirectPermanent(ancestorWithTemplate.Url)

    So you don't have to mess with HttpContext anymore

    Stephan

  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Feb 05, 2014 @ 10:43
    Ali Sheikh Taheri
    0

    Thank you Stephen, I've replaced it with SetRedirectPermanent and now the status code is 301 and that's exactly what I was looking for.

    Many Thanks Ali

  • Sören Deger 733 posts 2844 karma points c-trib
    Jan 07, 2015 @ 16:40
    Sören Deger
    0

    Hi all,

    I will keep the url, but this doesn't work for me in 7.2.1:

    request.PublishedContent= ancestorWithTemplate;

    I get the status code 404. But I can see on debugging that "ancestorWithTemplate" has the correct node resolved.

    Of course, this works for me but changed the url:

    request.SetRedirectPermanent(ancestorWithTemplate.Url);

    Can anyone help?


    Cheers

    Sören

Please Sign in or register to post replies

Write your reply to:

Draft