@@ -29,114 +29,76 @@ public bool TryFindContent(PublishedContentRequest docRequest)
29
29
30
30
#region Copied over and adapted from presentation.requestHandler
31
31
32
- void HandlePageNotFound ( PublishedContentRequest docRequest )
32
+ private static void HandlePageNotFound ( PublishedContentRequest docRequest )
33
33
{
34
34
var url = NotFoundHandlerHelper . GetLegacyUrlForNotFoundHandlers ( ) ;
35
35
LogHelper . Debug < ContentFinderByNotFoundHandlers > ( "Running for legacy url='{0}'." , ( ) => url ) ;
36
36
37
- foreach ( var handler in GetNotFoundHandlers ( ) )
37
+ foreach ( var handler in NotFoundHandlerHelper . GetNotFoundHandlers ( ) )
38
38
{
39
- IContentFinder finder = null ;
40
39
var handlerName = handler . GetType ( ) . FullName ;
41
-
42
40
LogHelper . Debug < ContentFinderByNotFoundHandlers > ( "Handler '{0}'." , ( ) => handlerName ) ;
43
41
44
- // replace with our own implementation
45
- if ( handler is global ::umbraco . SearchForAlias )
46
- finder = new ContentFinderByUrlAlias ( ) ;
47
- else if ( handler is global ::umbraco . SearchForProfile )
48
- finder = new ContentFinderByProfile ( ) ;
49
- else if ( handler is global ::umbraco . SearchForTemplate )
50
- finder = new ContentFinderByNiceUrlAndTemplate ( ) ;
51
- else if ( handler is global ::umbraco . handle404 )
52
- finder = new ContentFinderByLegacy404 ( ) ;
53
-
42
+ var finder = NotFoundHandlerHelper . SubsituteFinder ( handler ) ;
54
43
if ( finder != null )
55
44
{
56
45
var finderName = finder . GetType ( ) . FullName ;
57
46
LogHelper . Debug < ContentFinderByNotFoundHandlers > ( "Replace handler '{0}' by new finder '{1}'." , ( ) => handlerName , ( ) => finderName ) ;
58
- if ( finder . TryFindContent ( docRequest ) )
59
- {
60
- // do NOT set docRequest.PublishedContent again here as
61
- // it would clear any template that the finder might have set
62
- LogHelper . Debug < ContentFinderByNotFoundHandlers > ( "Finder '{0}' found node with id={1}." , ( ) => finderName , ( ) => docRequest . PublishedContent . Id ) ;
63
- if ( docRequest . Is404 )
64
- LogHelper . Debug < ContentFinderByNotFoundHandlers > ( "Finder '{0}' set status to 404." , ( ) => finderName ) ;
65
-
66
- // if we found a document, break, don't look at more handler -- we're done
67
- break ;
68
- }
69
-
70
- // if we did not find a document, continue, look at other handlers
71
- continue ;
72
- }
73
-
74
- // else it's a legacy handler, run
75
47
76
- if ( handler . Execute ( url ) && handler . redirectID > 0 )
77
- {
78
- var redirectId = handler . redirectID ;
79
- docRequest . PublishedContent = docRequest . RoutingContext . UmbracoContext . ContentCache . GetById ( redirectId ) ;
48
+ // can't find a document => continue with other handlers
49
+ if ( finder . TryFindContent ( docRequest ) == false )
50
+ continue ;
80
51
81
- if ( ! docRequest . HasPublishedContent )
82
- {
83
- LogHelper . Debug < ContentFinderByNotFoundHandlers > ( "Handler '{0}' found node with id={1} which is not valid." , ( ) => handlerName , ( ) => redirectId ) ;
84
- break ;
85
- }
52
+ // found a document => break, don't run other handlers, we're done
86
53
87
- LogHelper . Debug < ContentFinderByNotFoundHandlers > ( "Handler '{0}' found valid node with id={1}." , ( ) => handlerName , ( ) => redirectId ) ;
54
+ // in theory an IContentFinder can return true yet set no document
55
+ // but none of the substitued finders (see SubstituteFinder) do it.
88
56
89
- if ( docRequest . RoutingContext . UmbracoContext . HttpContext . Response . StatusCode == 404 )
90
- {
91
- LogHelper . Debug < ContentFinderByNotFoundHandlers > ( "Handler '{0}' set status code to 404." , ( ) => handlerName ) ;
92
- docRequest . Is404 = true ;
93
- }
57
+ // do NOT set docRequest.PublishedContent again here
58
+ // as it would clear any template that the finder might have set
94
59
95
- //// check for caching
96
- //if (handler.CacheUrl)
97
- //{
98
- // if (url.StartsWith("/"))
99
- // url = "/" + url;
60
+ LogHelper . Debug < ContentFinderByNotFoundHandlers > ( "Finder '{0}' found node with id={1}." , ( ) => finderName , ( ) => docRequest . PublishedContent . Id ) ;
61
+ if ( docRequest . Is404 )
62
+ LogHelper . Debug < ContentFinderByNotFoundHandlers > ( "Finder '{0}' set status to 404." , ( ) => finderName ) ;
100
63
101
- // var cacheKey = (currentDomain == null ? "" : currentDomain.Name) + url;
102
- // var culture = currentDomain == null ? null : currentDomain.Language.CultureAlias;
103
- // SetCache(cacheKey, new CacheEntry(handler.redirectID.ToString(), culture));
104
-
105
- // HttpContext.Current.Trace.Write("NotFoundHandler",
106
- // string.Format("Added to cache '{0}', {1}.", url, handler.redirectID));
107
- //}
108
-
109
- // if we found a document, break, don't look at more handler -- we're done
64
+ LogHelper . Debug < ContentFinderByNotFoundHandlers > ( "Handler '{0}' found valid node with id={1}." , ( ) => handlerName , ( ) => docRequest . PublishedContent . Id ) ;
110
65
break ;
111
66
}
112
67
113
- // if we did not find a document, continue, look at other handlers
114
- }
115
- }
68
+ // else it's a legacy handler: run
116
69
117
- IEnumerable < INotFoundHandler > GetNotFoundHandlers ( )
118
- {
119
- // instanciate new handlers
120
- // using definition cache
70
+ // can't find a document => continue with other handlers
71
+ if ( handler . Execute ( url ) == false || handler . redirectID <= 0 )
72
+ continue ;
121
73
122
- var handlers = new List < INotFoundHandler > ( ) ;
74
+ // found a document ID => ensure it's a valid document
75
+ var redirectId = handler . redirectID ;
76
+ docRequest . PublishedContent = docRequest . RoutingContext . UmbracoContext . ContentCache . GetById ( redirectId ) ;
123
77
124
- foreach ( var type in NotFoundHandlerHelper . CustomHandlerTypes )
125
- {
126
- try
78
+ if ( docRequest . HasPublishedContent == false )
127
79
{
128
- var handler = Activator . CreateInstance ( type ) as INotFoundHandler ;
129
- if ( handler != null )
130
- handlers . Add ( handler ) ;
80
+ // the handler said it could handle the url, and returned a content ID
81
+ // yet that content ID is invalid... should we run the other handlers?
82
+ // I don't think so, not here, let the "last chance" finder take care.
83
+ // so, break.
84
+
85
+ LogHelper . Debug < ContentFinderByNotFoundHandlers > ( "Handler '{0}' found node with id={1} which is not valid." , ( ) => handlerName , ( ) => redirectId ) ;
86
+ break ;
131
87
}
132
- catch ( Exception e )
88
+
89
+ // found a valid document => break, don't run other handlers, we're done
90
+
91
+ LogHelper . Debug < ContentFinderByNotFoundHandlers > ( "Handler '{0}' found valid node with id={1}." , ( ) => handlerName , ( ) => redirectId ) ;
92
+
93
+ if ( docRequest . RoutingContext . UmbracoContext . HttpContext . Response . StatusCode == 404 )
133
94
{
134
- LogHelper . Error < ContentFinderByNotFoundHandlers > ( string . Format ( "Error instanciating handler {0}, ignoring." , type . FullName ) , e ) ;
95
+ LogHelper . Debug < ContentFinderByNotFoundHandlers > ( "Handler '{0}' set status code to 404." , ( ) => handlerName ) ;
96
+ docRequest . Is404 = true ;
135
97
}
136
- }
137
98
138
- return handlers ;
139
- }
99
+ break ;
100
+ }
101
+ }
140
102
141
103
#endregion
142
104
}
0 commit comments