Skip to content

Commit 3b5c36e

Browse files
committed
[Static Web Assets] Allow assets with empty base paths
1 parent 6cdd41a commit 3b5c36e

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/Hosting/Hosting/src/StaticWebAssets/StaticWebAssetsFileProvider.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ public IDirectoryContents GetDirectoryContents(string subpath)
4646
{
4747
var modifiedSub = NormalizePath(subpath);
4848

49+
if (BasePath == "/")
50+
{
51+
return InnerProvider.GetDirectoryContents(modifiedSub);
52+
}
53+
4954
if (StartsWithBasePath(modifiedSub, out var physicalPath))
5055
{
5156
return InnerProvider.GetDirectoryContents(physicalPath.Value);
@@ -67,6 +72,11 @@ public IFileInfo GetFileInfo(string subpath)
6772
{
6873
var modifiedSub = NormalizePath(subpath);
6974

75+
if (BasePath == "/")
76+
{
77+
return InnerProvider.GetFileInfo(subpath);
78+
}
79+
7080
if (!StartsWithBasePath(modifiedSub, out var physicalPath))
7181
{
7282
return new NotFoundFileInfo(subpath);

src/Hosting/Hosting/test/StaticWebAssets/StaticWebAssetsFileProviderTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,35 @@ public void StaticWebAssetsFileProvider_FindsFileWithSpaces()
117117
Assert.True(provider.GetFileInfo("/_content/Static Web Assets.txt").Exists);
118118
}
119119

120+
[Fact]
121+
public void GetDirectoryContents_HandlesEmptyBasePath()
122+
{
123+
// Arrange
124+
var provider = new StaticWebAssetsFileProvider("/",
125+
Path.Combine(AppContext.BaseDirectory, "testroot", "wwwroot"));
126+
127+
// Act
128+
var directory = provider.GetDirectoryContents("/Static Web/");
129+
130+
// Assert
131+
Assert.Collection(directory,
132+
file =>
133+
{
134+
Assert.Equal("Static Web.txt", file.Name);
135+
});
136+
}
137+
138+
[Fact]
139+
public void StaticWebAssetsFileProviderWithEmptyBasePath_FindsFile()
140+
{
141+
// Arrange & Act
142+
var provider = new StaticWebAssetsFileProvider("/",
143+
Path.Combine(AppContext.BaseDirectory, "testroot", "wwwroot"));
144+
145+
// Assert
146+
Assert.True(provider.GetFileInfo("/Static Web Assets.txt").Exists);
147+
}
148+
120149
[Fact]
121150
public void GetFileInfo_DoesNotMatch_IncompletePrefixSegments()
122151
{

0 commit comments

Comments
 (0)