Skip to content

Ordered evaluation in GlobbingUrlBuilder.BuildUrlList #62134

Open
@kasperk81

Description

@kasperk81

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

The current GlobbingUrlBuilder.BuildUrlList does not evaluate include and exclude patterns in the order they are added, which leads to limitations when trying to achieve more complex file-matching behavior. Specifically, includes added after an exclude do not override the exclude, resulting in unexpected or undesired results.

Expected Behavior

The API should evaluate include/exclude paths in order.

Steps To Reproduce

$ dotnet new webapi -o webapi1
$ cd webapi1

Replace Program.cs with:

using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.AspNetCore.Mvc.TagHelpers;

var globBuilder = new GlobbingUrlBuilder(
    fileProvider: new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot")),
    cache: new MemoryCache(new MemoryCacheOptions()),
    requestPathBase: "/"
);

var urls = globBuilder.BuildUrlList(
    staticUrl: null,
    includePattern: "**/*",
    excludePattern: "ExcludeMe/**/*"
);

Console.WriteLine("Matched URLs:");
foreach (var url in urls)
{
    Console.WriteLine(url);
}

make wwwroot structure:

$ mkdir -p wwwroot/ExcludeMe/ButActuallyIncludeMe
$ echo 'Hello' > wwwroot/helloWorld.txt
$ echo 'Do not show me' > wwwroot/ExcludeMe/notIncluded.txt
$ echo 'Include me!' > wwwroot/ExcludeMe/ButActuallyIncludeMe/hiEarth.txt

run:

$ dotnet run
...
Matched URLs:
/helloWorld.txt

Note, it is missing ExcludeMe/ButActuallyIncludeMe/hiEarth.txt

Exceptions (if any)

No response

.NET Version

9.0.203

Anything else?

Once this API is available dotnet/runtime#109408 (comment), change

var matcher = MatcherBuilder != null ? MatcherBuilder() : new Matcher();

-        var matcher = MatcherBuilder != null ? MatcherBuilder() : new Matcher();
+        var matcher = MatcherBuilder != null ? MatcherBuilder() : new Matcher(preserveFilterOrder: true);

or add bool preserveFilterOrder = false argument in BuildUrlList as well if existing evaluation order must be kept.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templates

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions