Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

asp.net core - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Have a look at how PinpointTownes implemented this in his OpenIddict project.

You can use the UseStaticFiles call with a EmbeddedFileProvider. It's part of the rc1-final package, as you can see here. The relevant code is found 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 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...