简体   繁体   English

如何在 Visual Studio 2015 中的一个解决方案中跨多个 ASP.NET CORE 项目共享 appsettings.json 等配置文件?

[英]How can I share config file like appsettings.json across multiple ASP.NET CORE projects in one solution in Visual Studio 2015?

I have 3 ASP.NET CORE projects in one solution and I want to share connection string information in one config file like appsettings.json across these projects.我在一个解决方案中有 3 个 ASP.NET CORE 项目,我想在这些项目之间共享一个配置文件(如 appsettings.json)中的连接字符串信息。

How can I do this in Visual Studio 2015 ?如何在 Visual Studio 2015 中执行此操作?

You can use absolute path on each project like this(I assume appsettings.json file is in the solution root directory(like global.json) ):您可以像这样在每个项目上使用绝对路径(我假设appsettings.json文件位于解决方案根目录中(如 global.json)):

var settingPath = Path.GetFullPath(Path.Combine(@"../../appsettings.json")); // get absolute path
var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile(settingPath);

see https://github.com/aspnet/Configuration/issues/440https://github.com/aspnet/Configuration/issues/440

Set the absolute path to the shared appsettings.json file.设置共享 appsettings.json 文件的绝对路径。

var relativePath = @"../../The/Relative/Path";
var absolutePath = System.IO.Path.GetFullPath(relativePath);

Then use a IFileProvider like this.然后像这样使用IFileProvider

var fileProvider = 
    new Microsoft.Extensions.FileProviders.PhysicalFileProvider(absolutePath);

_configuration = new ConfigurationBuilder()
    .SetBasePath(_env.ContentRootPath)
    .AddJsonFile(fileProvider, $"appsettings.json", optional: true, reloadOnChange: true)
    .Build();

Or use SetBasePath like this.或者像这样使用SetBasePath

var relativePath = @"../../The/Relative/Path";
var absolutePath = System.IO.Path.GetFullPath(relativePath);

_configuration = new ConfigurationBuilder()
    .SetBasePath(absolutePath)
    .AddJsonFile($"appsettings.json", optional: true, reloadOnChange: true)
    .Build();

Both will work, though the file provider approach allows the app to use its content root as the base path for other static content.尽管文件提供程序方法允许应用程序使用其内容根目录作为其他静态内容的基本路径,但两者都可以工作。 Adem's approach is also good. Adem的做法也不错。

Another option is to use user secrets and have the apps share the same app id.另一种选择是使用用户机密并让应用共享相同的应用 ID。

As a bonus, you get the keep the connection strings outside of your app and decrease the chances that they will leak because they were pushed in source control.作为奖励,您可以将连接字符串保留在您的应用程序之外,并减少它们泄漏的机会,因为它们是在源代码控制中推送的。

There may be a better way to do this, but you could achieve this by adding a precompile setting to the project.json file.可能有更好的方法来执行此操作,但您可以通过向 project.json 文件添加预编译设置来实现此目的。 Something similar to:类似于:

"scripts": {
  "precompile": [ "../copycommand.bat" ]
}

Where the copycommand points to a batch command that copies your AppSettings.json file into the current directory.其中copycommand指向将 AppSettings.json 文件复制到当前目录的批处理命令。 If you're targeting multiple platforms, you'll likely need to customise the command for each of the platforms.如果您的目标是多个平台,您可能需要为每个平台自定义命令。

添加一个包含 appsettings.json 文件的共享项目,该文件设置为“如果较新则复制”并从其他项目中引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM