Skip to content

Commit 3211894

Browse files
committed
When looking up a topic by RouteData, swallow InvalidKeyException
As route data comes from user-submitted requests, it's expected that some may contain invalid keys. From that perspective, an invalid key is no different than having an invalid URL. As such, we should return a null result, not bubble up an exception. In practical terms, that means that calling e.g. `/Web/Invalid/Url` and calling `/Web/Invalid/~Url` will both end up returning a 404 error when processed by the `[ValidateTopic]` attribute. By contrast, previously, the former would return a 404 while the latter would return a 500 error due to the `InvalidKeyException`.
1 parent 0edab34 commit 3211894

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

OnTopic.AspNetCore.Mvc/TopicRepositoryExtensions.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,14 @@ RouteData routeData
7979
foreach (var searchPath in paths) {
8080
if (topic is not null) break;
8181
if (String.IsNullOrEmpty(searchPath)) continue;
82-
topic = topicRepository.Load(searchPath);
82+
try {
83+
topic = topicRepository.Load(searchPath);
84+
}
85+
catch (InvalidKeyException) {
86+
//As route data comes from user-submitted requests, it's expected that some may contain invalid keys. From this
87+
//method's perspective, this is no different than having an invalid URL. As such, we should return a null result, not
88+
//bubble up an exception.
89+
}
8390
}
8491

8592
return topic;

0 commit comments

Comments
 (0)