@@ -58,54 +58,75 @@ public void Remove(CustomRedirect customRedirect)
5858 // TODO: If desired, change parameters to Find method to search based on a property of CustomRedirect.
5959 public CustomRedirect Find ( Uri urlNotFound )
6060 {
61- string pathAndQuery = HttpUtility . HtmlEncode ( urlNotFound . PathAndQuery ) ;
61+ // Handle absolute addresses first
62+ string url = urlNotFound . AbsoluteUri ;
63+ CustomRedirect foundRedirect = FindInternal ( url ) ;
6264
63- object foundRedirect = _quickLookupTable [ urlNotFound . AbsoluteUri ] ?? _quickLookupTable [ pathAndQuery ] ;
64- if ( foundRedirect ! = null )
65+ // Common case
66+ if ( foundRedirect = = null )
6567 {
66- return foundRedirect as CustomRedirect ;
68+ url = urlNotFound . PathAndQuery ; ;
69+ foundRedirect = FindInternal ( url ) ;
6770 }
68- else
71+
72+ // Handle legacy databases with encoded values
73+ if ( foundRedirect == null )
6974 {
70- // No exact match could be done, so we'll check if the 404 url
71- // starts with one of the urls we're matching against. This
72- // will be kind of a wild card match (even though we only check
73- // for the start of the url
74- // Example: http://www.mysite.com/news/mynews.html is not found
75- // We have defined an "<old>/news</old>" entry in the config
76- // file. We will get a match on the /news part of /news/myne...
77- // Depending on the skip wild card append setting, we will either
78- // redirect using the <new> url as is, or we'll append the 404
79- // url to the <new> url.
80- IDictionaryEnumerator _enumerator = _quickLookupTable . GetEnumerator ( ) ;
81- while ( _enumerator . MoveNext ( ) )
82- {
83- // See if this "old" url (the one that cannot be found) starts with one
84- if ( pathAndQuery . StartsWith ( _enumerator . Key . ToString ( ) , StringComparison . InvariantCultureIgnoreCase ) )
85- {
86- foundRedirect = _quickLookupTable [ _enumerator . Key ] ;
87- CustomRedirect cr = foundRedirect as CustomRedirect ;
88- if ( cr . WildCardSkipAppend == true )
89- {
90- // We'll redirect without appending the 404 url
91- return cr ;
92- }
93- else
94- {
95- // We need to append the 404 to the end of the
96- // new one. Make a copy of the redir object as we
97- // are changing it.
98- CustomRedirect redirCopy = new CustomRedirect ( cr ) ;
99- redirCopy . NewUrl = redirCopy . NewUrl + pathAndQuery . Substring ( _enumerator . Key . ToString ( ) . Length ) ;
100- return redirCopy ;
101- }
102- }
103- }
75+ url = HttpUtility . HtmlEncode ( url ) ;
76+ foundRedirect = FindInternal ( url ) ;
10477 }
105- return null ;
78+
79+ return foundRedirect ;
10680 }
10781
108- public CustomRedirect FindInProviders ( string oldUrl )
82+ private CustomRedirect FindInternal ( string url )
83+ {
84+ object foundRedirect = _quickLookupTable [ url ] ;
85+ if ( foundRedirect != null )
86+ {
87+ return foundRedirect as CustomRedirect ;
88+ }
89+ else
90+ {
91+ // No exact match could be done, so we'll check if the 404 url
92+ // starts with one of the urls we're matching against. This
93+ // will be kind of a wild card match (even though we only check
94+ // for the start of the url
95+ // Example: http://www.mysite.com/news/mynews.html is not found
96+ // We have defined an "<old>/news</old>" entry in the config
97+ // file. We will get a match on the /news part of /news/myne...
98+ // Depending on the skip wild card append setting, we will either
99+ // redirect using the <new> url as is, or we'll append the 404
100+ // url to the <new> url.
101+ IDictionaryEnumerator _enumerator = _quickLookupTable . GetEnumerator ( ) ;
102+ while ( _enumerator . MoveNext ( ) )
103+ {
104+ // See if this "old" url (the one that cannot be found) starts with one
105+ if ( url . StartsWith ( _enumerator . Key . ToString ( ) , StringComparison . InvariantCultureIgnoreCase ) )
106+ {
107+ foundRedirect = _quickLookupTable [ _enumerator . Key ] ;
108+ CustomRedirect cr = foundRedirect as CustomRedirect ;
109+ if ( cr . WildCardSkipAppend == true )
110+ {
111+ // We'll redirect without appending the 404 url
112+ return cr ;
113+ }
114+ else
115+ {
116+ // We need to append the 404 to the end of the
117+ // new one. Make a copy of the redir object as we
118+ // are changing it.
119+ CustomRedirect redirCopy = new CustomRedirect ( cr ) ;
120+ redirCopy . NewUrl = redirCopy . NewUrl + url . Substring ( _enumerator . Key . ToString ( ) . Length ) ;
121+ return redirCopy ;
122+ }
123+ }
124+ }
125+ }
126+ return null ;
127+ }
128+
129+ public CustomRedirect FindInProviders ( string oldUrl )
109130 {
110131 // If no exact or wildcard match is found, try to parse the url through the custom providers
111132 if ( Bvn404HandlerConfiguration . Instance . Bvn404HandlerProviders != null || Bvn404HandlerConfiguration . Instance . Bvn404HandlerProviders . Count != 0 )
0 commit comments