Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include wwwroot from a library project?

Tags:

asp.net-core

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?

like image 358
hultqvist Avatar asked Mar 24 '16 11:03

hultqvist


People also ask

How do I add wwwroot to project?

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.

Where do I put 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.

Is wwwroot a folder?

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.

How do you make a Razor class library?

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 .


1 Answers

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.

like image 179
Tseng Avatar answered Oct 27 '22 17:10

Tseng