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 PublishedContentRequestPrepared event...
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.
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.
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
Hi Ali,
With your
IContentFinder
, I'm not sure how you are inserting it into theContentFinderResolver
. 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...Then check if the node has a template associated with it...
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.
Good luck!
Cheers,
- Lee
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
I added the event to
Umbraco.Core.ApplicationEventHandler.ApplicationStarted
.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 )
Many Thanks
Ali
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.
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
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.
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. :(
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
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
Hi all,
I will keep the url, but this doesn't work for me in 7.2.1:
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:
Can anyone help?
Cheers
Sören
is working on a reply...