asp.net core and including shared content files

2019-06-26 20:56发布

问题:

I have a Shared Project project which contains some content files like JSON, etc.

I have a number of ASP.Net core projects and I want to include/share some of these JSON files. Very simple with the "Link to File" option in VS but it's not available in ASP.Net core projects.

I've tried playing with the buildOptions/copyToOutput as follows but this does not seem to be working and no error or warning logged in the build process.

"copyToOutput": {
    "include": [ "../Common.Includes/mysettings*.json" ] }

Any ideas gratefully accepted?

Thanks

Donal

回答1:

The copyToOutput setting works fine for me (at least in dotnet 1.0.0-preview3-003171 on Windows). Note however that copyToOutput is not transitive - meaning tath if the files are copied in project A, they won't be copied to output in project B that references project A.

Another method is to use underlying file system and create a symbolic link to the shared file or folder. On Linux it would be:

> ln -s ../Common.Includes common

Windows has a similar (but less commonly used) feature:

> mklink /J common ..\Common.Includes

You can then configure dotnet to run it on a specific event:

"scripts": {
    "precompile": "cmd /C mklink /J common ..\\Common.Includes"
}

In RC1 there were also events like 'prepare' or 'prerestore', which would be more suitable for this, but for now they're gone and dotnet only supports 'precompile' and 'postcompile'.

It will be even better to put this command into a separate script along with some logic to check if the link already exists. On windows, I would create init.ps1 Powershell script:

if (!(Test-Path "common")) {
    cmd /C mklink /J "common" "..\Common.Includes"
}

Then reference it from "scripts":

"scripts": {
    "precompile": "init.ps1"
}