Skip to content

Instantly share code, notes, and snippets.

@joanjane
Last active September 19, 2018 08:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joanjane/a89de7c1135cb398c2e7434fecc9caeb to your computer and use it in GitHub Desktop.
Save joanjane/a89de7c1135cb398c2e7434fecc9caeb to your computer and use it in GitHub Desktop.
.NET Framework development settings

Using settings for development on netframework:

If you need a working way to use development settings like .net core does with appsettings.json and appsettings.Development.json, you can follow this steps.

  1. Install nuget package MSBuild.Microsoft.VisualStudio.Web.targets dependency on your executable project to transform config files.

  2. Edit your .csproj file on executable project.

  3. Add your App.Debug.config as below (in case of being a web application, Web.config & Web.Debug.config). This adds a nice tree list on VS of these two files:

  <ItemGroup>
    <None Include="App.config" />
    <None Include="App.Debug.config">
      <DependentUpon>App.config</DependentUpon>
    </None>
    ...
  </ItemGroup>
  1. Put this before first <ItemGroup> ONLY if your project use an App.config instead of Web.config (ex.: console applications):
  <PropertyGroup>
    <ProjectConfigFileName>App.config</ProjectConfigFileName>
  </PropertyGroup>
  1. Put this before closing </Project>. This adds a task to transform the the App.config/Web.config to App.{CurrentConfig}.config if exists:
  <Target Name="AfterBuild" Condition="Exists('./$(ProjectConfigTransformFileName)')">
    <TransformXml Source="@(AppConfigWithTargetPath)" Transform="$(ProjectConfigTransformFileName)" Destination="@(AppConfigWithTargetPath->'$(OutDir)%(TargetPath)')" />
  </Target>
  1. Use App.Debug.config/Web.Debug.config:
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <connectionStrings>
    <!-- overwrite connection string -->
	  <add name="DbConnectionString" value="..." xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />
  </connectionStrings>
  <appSettings>
    <!-- overwrite setting -->
    <add key="Environment" value="development" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
    <!-- insert new setting -->
    <add key="LogLevel" value="0" xdt:Transform="Insert" />
  </appSettings>
</configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment