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.
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?
In order to add the wwwroot folder, right-click on the project and then select add => new folder option and then provide the folder name as wwwroot.
The default directory for static files is wwwroot and this directory must be in the root project folder. After clicking next, another wizard will open. Under project name give a meaningful name to your project and click on create.
By default, the wwwroot folder in the ASP.NET Core project is treated as a web root folder. Static files can be stored in any folder under the web root and accessed with a relative path to that root.
Create a class library containing Razor UI From Visual Studio select Create a new project. Select Razor Class Library > Next. Name the library (for example, "RazorClassLib"), > Create > Next. To avoid a file name collision with the generated view library, ensure the library name doesn't end in .
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With