Skip to content

[Static Web Assets] Allow assets with empty base paths #17414

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public IDirectoryContents GetDirectoryContents(string subpath)
{
var modifiedSub = NormalizePath(subpath);

if (BasePath == "/")
{
return InnerProvider.GetDirectoryContents(modifiedSub);
}

if (StartsWithBasePath(modifiedSub, out var physicalPath))
{
return InnerProvider.GetDirectoryContents(physicalPath.Value);
Expand All @@ -67,6 +72,11 @@ public IFileInfo GetFileInfo(string subpath)
{
var modifiedSub = NormalizePath(subpath);

if (BasePath == "/")
{
return InnerProvider.GetFileInfo(subpath);
}

if (!StartsWithBasePath(modifiedSub, out var physicalPath))
{
return new NotFoundFileInfo(subpath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,35 @@ public void StaticWebAssetsFileProvider_FindsFileWithSpaces()
Assert.True(provider.GetFileInfo("/_content/Static Web Assets.txt").Exists);
}

[Fact]
public void GetDirectoryContents_HandlesEmptyBasePath()
{
// Arrange
var provider = new StaticWebAssetsFileProvider("/",
Path.Combine(AppContext.BaseDirectory, "testroot", "wwwroot"));

// Act
var directory = provider.GetDirectoryContents("/Static Web/");

// Assert
Assert.Collection(directory,
file =>
{
Assert.Equal("Static Web.txt", file.Name);
});
}

[Fact]
public void StaticWebAssetsFileProviderWithEmptyBasePath_FindsFile()
{
// Arrange & Act
var provider = new StaticWebAssetsFileProvider("/",
Path.Combine(AppContext.BaseDirectory, "testroot", "wwwroot"));

// Assert
Assert.True(provider.GetFileInfo("/Static Web Assets.txt").Exists);
}

[Fact]
public void GetFileInfo_DoesNotMatch_IncompletePrefixSegments()
{
Expand Down