'Include wwwroot from a library project?

I'm working on a project structure with multiple projects serving the same set of static files.

At start each project will server both the static files and the API services but later on I plan to separate some of them into multiple projects.

  • ProjectA will server both static files and API
  • ProjectB1 will only serve the same static files as in ProjectA
  • ProjectB2 will only serve the API

In a classic VS library you can have files marked as content. These will be included in the build output of any project that references that library.

I tried to make a project ProjectStatic containing the static files and reference it from both ProjectA and ProjectB1 but none of the files in ProjectStatic are included in the output of ProjectA nor ProjectB1.

Can this be done using project.json?



Solution 1:[1]

You can use the UseStaticFiles call with a EmbeddedFileProvider. It's part of the rc1-final package, as you can see here.

Just for future readers:

app.UseStaticFiles(new StaticFileOptions {
        FileProvider = new EmbeddedFileProvider(
            assembly: Assembly.Load(new AssemblyName("OpenIddict.Assets")),
            baseNamespace: "OpenIddict.Assets")
});

OpenIddict.Assets is the assembly/project name that contains the static resources.

Update:

After digging a bit through the source and finding the right repository, there is also a PhysicalFileProvider you may be able to use instead of packing it into the assembly and point to an arbitrary folder on the file system.

app.UseStaticFiles(new StaticFileOptions {
        FileProvider = new PhysicalFileProvider("/path/to/shared/staticfiles")
});

Update 2:

Just for the sake of completeness, there is also a CompositeFileProvider which you could use to have multiple IFileProviders to create some kind of virtual file system structure, i.e. if the file is not found in the PhysicalFileProvider given location, load it from an EmbeddedFileProvider.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Emre