Common wwwroot folder for multiple websites in MVC

2019-07-07 02:31发布

I have multiple websites which use /wwwroot/assets folder (html theme, css and javascript files) to load the static content.

Currently I am copying assets folder in each site. All of my projects are sitting under a common parent directory.

I don't want to copy the /wwwroot/assets folder into each website. Is there a way to share one assets folder between all sites. May be by providing a direct file system path or something?

1条回答
甜甜的少女心
2楼-- · 2019-07-07 02:45

At the moment it's not clear from the documentation what sorts of values the webroot key in the project.json file will accept, but so far it would appear that Visual Studio doesn't care for very complicated paths. For example, setting the value to ../wwwroot causes the entry to disappear in the Solution Explorer.

If you look at the kpm code that bundles your project up for deployment, it appears to combine your project's directory with whatever is stored in the wwwroot key, so even though Visual Studio may not understand it, relative paths appear to be supported. Using kpm bundle from the command line confirms this, and a directory above src bundles correctly when using a relative path.

Depending on your particular needs, there is one way that should work that makes kpm and Visual Studio happy, but it will depend on your build environment as to whether that is a good option for you.

Windows, OSX, and Linux all support creating symbolic links for directories, which would allow you to have your assets directory in one location in the filesystem and then create links to it elsewhere. For example, if you had assets in /projects/shared/assets, you could create a link in both of your other projects (e.g. /projects/project1/src/wwwroot/assets) that point to the "real" location.

In Windows, the command would might something like this

mklink /j "C:\link\to\create" "C:\path\to\assets"

So if you did

mklink /j "C:\source\shared\assets" "C:\source\project1\src\wwwroot\assets"

project1 would appear to have an assets directory inside of wwwroot and the build process would be happy since it would appear to each project that the files were local. One thing to note here is that Windows supports a number of different sorts of links. /j specifically creates a junction rather than a true symbolic link. The differences are a bit subtle, but this is a good description of the differences. It is enough to know that if you're working locally, the /j command doesn't require administrative rights and Visual Studio and kpm will both be happy.

In OSX and Linux, the command is similar:

ln -s /link/to/create /path/to/assets

and like Windows, they support different sorts of links.

In any case, under the right circumstances, this might work well without needing any special support from the new ASP.NET project structure, but it would be nice to eventually have that as well.

查看更多
登录 后发表回答