Optional appsettings.local.json in (new format) visual studio project

时间秒杀一切 提交于 2019-12-05 08:02:52

With v2 this is dead simple.

  1. Add an appsettings.local.json to your project (it should nest itself below the main appsettings.json file).
  2. Add appsettings.local.json to your .gitignore
  3. In your startup.cs within the constructor do the following:

    public class Startup
    {
        public IConfigurationRoot Configuration { get; }
    
        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) //load base settings
                .AddJsonFile("appsettings.local.json", optional: true, reloadOnChange: true) //load local settings
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) //load environment settings
                .AddEnvironmentVariables();
    
            Configuration = builder.Build();
        }
    
        /*
         * rest of your startup.cs
         */
    }
    

The plan:

  1. Place default local values to appsettings.local-base.json.
  2. Add appsettings.local.json to .gitignore.
  3. Copy appsettings.local-base.json as appsettings.local.json to output folder if appsettings.local.json doesn't exist.
  4. Do nothing if user has his own appsettings.local.json in project folder (it will be copied to output folder by VS).

MSBuild Copy target can conditionally copy a file before/after build. The target below is actual for a Visual Studio 2017 and csproj-based .NET Core project (file names are reduced):

<Project Sdk="Microsoft.NET.Sdk.Web">

    <PropertyGroup>
    <TargetFramework>netcoreapp1.1</TargetFramework>
    </PropertyGroup>

    <!--rest of file -->  

    <Target Name="TestTarget" AfterTargets="Build">
        <ItemGroup>
            <FromFile Include="src.json" />
            <ToFile Include="$(OutDir)dest.json" />
        </ItemGroup>

        <Message Text="Copying @(FromFile) file to: @(ToFile)" Importance="high" />

        <Copy   
            SourceFiles="@(FromFile)" 
            DestinationFiles="@(ToFile)" 
            Condition="!Exists('@(ToFile)')" 
            OverwriteReadOnlyFiles="true" 
            SkipUnchangedFiles="false" />
    </Target>  
</Project>

After the project build the following message should appear in VS build output:

Copying src.json file to: bin\Debug\netcoreapp1.1\dest.json

Seems like this stuff is changing so fast, it's hard to keep track of "the right way" Microsoft wants these things handled.

In ASP.NET Core 2, I believe the expected way for developers to handle per-developer configuration values is with the Secret Manager.

Short summary: In Visual Studio 2019, right-click the project and choose Manage User Secrets. This creates a JSON file in %APPDATA%\Microsoft\UserSecrets\... that is picked up automatically by WebHost.CreateDefaultBuilders() (which is called implicitly in default ASP.NET core projects).

For .Net Core >2.1 you can simply chain the extension method ConfigureAppConfiguration(Action<WebHostBuilderContext, IConfigurationBuilder> configureDelegate) to your WebHost. Here is an example:

WebHost.CreateDefaultBuilder(args)
            .UseApplicationInsights()
            .ConfigureAppConfiguration((hostingContext, config) =>
            {
                config.AddJsonFile("appsettings.Local.json", optional: true, reloadOnChange: true); //load Local settings
            })

And of course ignore the appsettings.Local.json in your .gitignore.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!