Search code examples
fileasp.net-corestatic-filesasp.net-core-staticfile

Is changing WebRootFileProvider a proper way to apply custom IFileProvider to asp-append-version? Any side-effect?


I want to be able to serve files with a custom file path (js/v1/foo.js can be mapped to a real file at wwwroot/js/foo.js). I created VersionedFileProvider which could map that already. I set it like this:

        app.UseStaticFiles(new StaticFileOptions()
        {
            FileProvider = new CompositeFileProvider(
                new VersionedFileProvider(env.WebRootPath),
                env.WebRootFileProvider),
        });

All files are working. However, if I use that custom path, asp-append-version does not work. When I check the source code, they are taking WebRootFileProvider instead of the one I am setting in UseStaticFiles.

Luckily, env.WebRootFileProvider is settable, so I can resolve the problem with:

        env.WebRootFileProvider = new CompositeFileProvider(
            new VersionedFileProvider(env.WebRootPath),
            env.WebRootFileProvider);
        app.UseStaticFiles();

However, I have to admit I do not know what else is using WebRootFileProvider and what side-effect can possibly happen. Please help me confirm this is a proper way to resolve my issue.


Solution

  • Confirm from development team the way I suggested in the question is recommended

        env.WebRootFileProvider = new CompositeFileProvider(
            new VersionedFileProvider(env.WebRootPath),
            env.WebRootFileProvider);
        app.UseStaticFiles();