Include wwwroot from library project?

I am working on a project structure with several projects serving the same set of static files.

At startup, each project will serve both static files and API services, but later I plan to separate some of them from several projects.

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

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

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

Can this be done using project.json?

+4
source share
1 answer

See how PinpointTownes implemented this in their OpenIddict project.

You can use the UseStaticFiles call with the EmbeddedFileProvider . This is part of the rc1-final package, as you can see here . The corresponding code is on GitHub .

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 name of the assembly / project containing static resources.

Update:

After you dig a bit through the source and find the correct repository, there is also PhysicalFileProvider , which you can use instead of packing into an assembly and point to an arbitrary folder in the file system.

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

Update 2:

Just for completeness, there is also a CompositeFileProvider that can be used to create several IFileProviders to create some kind of virtual file system structure, that is, if the file is not found in the PhysicalFileProvider specified location, download it from EmbeddedFileProvider .

+4
source

All Articles