@@ -48,32 +48,34 @@ public DefaultFilesMiddleware([NotNull] RequestDelegate next, [NotNull] IHosting
48
48
/// <returns></returns>
49
49
public Task Invoke ( HttpContext context )
50
50
{
51
- IEnumerable < IFileInfo > dirContents ;
52
51
PathString subpath ;
53
52
if ( Helpers . IsGetOrHeadMethod ( context . Request . Method )
54
- && Helpers . TryMatchPath ( context , _matchUrl , forDirectory : true , subpath : out subpath )
55
- && _options . FileSystem . TryGetDirectoryContents ( subpath . Value , out dirContents ) )
53
+ && Helpers . TryMatchPath ( context , _matchUrl , forDirectory : true , subpath : out subpath ) )
56
54
{
57
- // Check if any of our default files exist.
58
- for ( int matchIndex = 0 ; matchIndex < _options . DefaultFileNames . Count ; matchIndex ++ )
55
+ var dirContents = _options . FileSystem . GetDirectoryContents ( subpath . Value ) ;
56
+ if ( dirContents . Exists )
59
57
{
60
- string defaultFile = _options . DefaultFileNames [ matchIndex ] ;
61
- IFileInfo file ;
62
- // TryMatchPath will make sure subpath always ends with a "/" by adding it if needed.
63
- if ( _options . FileSystem . TryGetFileInfo ( subpath + defaultFile , out file ) )
58
+ // Check if any of our default files exist.
59
+ for ( int matchIndex = 0 ; matchIndex < _options . DefaultFileNames . Count ; matchIndex ++ )
64
60
{
65
- // If the path matches a directory but does not end in a slash, redirect to add the slash.
66
- // This prevents relative links from breaking.
67
- if ( ! Helpers . PathEndsInSlash ( context . Request . Path ) )
61
+ string defaultFile = _options . DefaultFileNames [ matchIndex ] ;
62
+ var file = _options . FileSystem . GetFileInfo ( subpath + defaultFile ) ;
63
+ // TryMatchPath will make sure subpath always ends with a "/" by adding it if needed.
64
+ if ( file . Exists )
68
65
{
69
- context . Response . StatusCode = 301 ;
70
- context . Response . Headers [ Constants . Location ] = context . Request . PathBase + context . Request . Path + "/" + context . Request . QueryString ;
71
- return Constants . CompletedTask ;
72
- }
66
+ // If the path matches a directory but does not end in a slash, redirect to add the slash.
67
+ // This prevents relative links from breaking.
68
+ if ( ! Helpers . PathEndsInSlash ( context . Request . Path ) )
69
+ {
70
+ context . Response . StatusCode = 301 ;
71
+ context . Response . Headers [ Constants . Location ] = context . Request . PathBase + context . Request . Path + "/" + context . Request . QueryString ;
72
+ return Constants . CompletedTask ;
73
+ }
73
74
74
- // Match found, re-write the url. A later middleware will actually serve the file.
75
- context . Request . Path = new PathString ( context . Request . Path . Value + defaultFile ) ;
76
- break ;
75
+ // Match found, re-write the url. A later middleware will actually serve the file.
76
+ context . Request . Path = new PathString ( context . Request . Path . Value + defaultFile ) ;
77
+ break ;
78
+ }
77
79
}
78
80
}
79
81
}
0 commit comments