I have a Web.config file with some entries in there but depending on whether I am in Debug or Release mode when I execute my Web Application locally, I want to take different appSettings.
For instance, let's say that I have the following entry in my Web.Debug.config appSettings.
<add key="MyServiceUrl" value="http://my-test-site:8080/" />
And also I have this in my Web.Release.config:
<add key="MyServiceUrl" value="http://my-prod-site:80/" />
How should I configure my Web.Config, Web.Debug.Config and Web.Release.Config so depending on the mode I run my application locally (Debug - Any CPU vs. Release - Any CPU), it takes the right one?
Right now, the only pair of key and value that it takes is the one from the main Web.Config regardless I select Debug or Release in Visual Studio.
How can I configure that behavior?
EDIT
This is how I have defined Web.config
<appSettings>
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
This is how I have defined Web.Debug.config
<appSettings>
<add key="MyServiceUrl" value="http://my-test-site:8080/" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
</appSettings>
This is how I have defined Web.Release.config
<appSettings>
<add key="MyServiceUrl" value="http://my-prod-site:8080/" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
</appSettings>
Finally, in my code, I have the following method:
public static string GetAppSetting(string settingKey)
{
if (string.IsNullOrEmpty(settingKey))
throw new System.ArgumentException("Cannot fetch a setting for a null/empty setting key.");
return ConfigurationManager.AppSettings[settingKey];
}
which I call it like this:
string setting = GetAppSetting("MyServiceUrl");
However, it is null if it is not defined in the main Web.config
In the web.Release.config try this, it should work:
<appSettings>
<add key="MyServiceUrl" value="http://my-prod-site:8080/" xdt:Transform="Insert" />
</appSettings>
Read this: Web.config Transformation Syntax for Web Application Project Deployment
Related
I have the following in my web.config located at the root of my project:
<configuration>
<connectionStrings>
<clear />
<add name="Default" providerName="System.Data.SqlClient" connectionString="Server=tcp:whoops;Encrypt=True;TrustServerCertificate=False;Connection Timeout=3000;" />
</connectionStrings>
<appSettings>
<add key="ConnectionString" value="test"/>
</appSettings>
....
I read from Startup.cs (this is an asp.net core web app):
string connection = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
However when I break on this, ConfigurationManager.ConnectionStrings and ConfigurationManager.AppSettings are empty (well, the first has some default connection string that is not the one in web.config).
What's going on here?
You will have to migrate the config to the new file appsettings.json
https://learn.microsoft.com/en-us/aspnet/core/migration/configuration?view=aspnetcore-2.1
Not saying this is how you should do it, but you can do the following...
In ASP.Net Core 2.2, you should be able to add an XML configuration to IConfigurationBuilder using
configBuilder.AddXmlFile("app.config");
Contents is pretty much the same as above...
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings configSource="connectionStrings.config" />
<appSettings>
<add key="Test" value="TestValue" />
</appSettings>
</configuration>
You should then be able to access AppSettings/ConnectionStrings using...
ConfigurationManager.ConnectionStrings
ConfigurationManager.AppSettings.AllKeys
{string[1]}
[0]: "Test"
ConfigurationManager.AppSettings.Get("Test")
"TestValue"
I have created a new mvc webapp with the following transforms for web.config:
Web.Debug.config
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="Debug" value="true" xdt:Transform="Insert"/>
</appSettings>
</configuration>
Web.Release.config
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="Release" value="true" xdt:Transform="Insert"/>
</appSettings>
</configuration>
Then I created a publish profile and called it Release, but I select the Debug(OBS! important) build configuration.
(I know. Stupid example. In my real project they were called Test and Test2.)
When I run the publish action I get the following in the transformed Web.config:
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="Debug" value="true"/>
<add key="Release" value="true"/>
</appSettings>
Both the transformations were performed! Strange! If I change the name of the publish profile to Release2 I get following correct result:
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="Debug" value="true"/>
</appSettings>
What do you think? Bug?
Met same issue and finally was able to identify the reason and resolve it.
This issue happens because of publish profiles configuration settings mess. When you set up profile with configuration manager you should make sure Current Solution Configuration matches Configuration that is going to be used: Easiest way for configuration to track config Transformations
Otherwise, remember, that before selected Profile configuration, selected Configuration Transformation would be applied.
So, make sure you don't have twice assigned Configuration for different profiles. If so, just add one more configuration for 'failed' profile to resolve it and you get what you expected on transformation applied.
I have a configuration in my web.config like this?
<configuration>
<appSettings>
<add key="BASE_URL" value="/mvc/" />
</appSettings>
</configuration>
in my web.Release.config, I have this:
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="BASE_URL" value="/mvc" xdt:Transform="Replace" xdt:Locator="Match(key)"/>
</appSettings>
</configuration>
but, when I call BASE_URL property, it's return "/mvc/" and not "/mvc"
I using the Release Configuration to build my project
I using C# MVC4 in VS 2013
Note:
I've searched for this on Google and didn't directly find anything related to WinForms. The suggested questions while typing this question didn't apply to my situation.
I have a WinForms (.NET 3.5) application that can be published/deployed to three different locations: development, test and production. Each deployment type has its own specific settings, for example: connectionString, title(s), webservice address, directories/files to read or other settings.
The current situation in the app.config is as following:
<!-- dev-->
<configuration>
<configSections>...</configSections>
<appSettings>
<add key="Deployment" value="dev" />
<add key="SourceDir" value="\\server\foo\dev" />
</appSettings>
...
</configuration>
<!-- test -->
<configuration>
<configSections>...</configSections>
<appSettings>
<add key="Deployment" value="test" />
<add key="SourceDir" value="\\server\foo\test" />
</appSettings>
...
</configuration>
<!-- prod -->
<configuration>
<configSections>...</configSections>
<appSettings>
<add key="Deployment" value="prod" />
<add key="SourceDir" value="\\server\foo\prod" />
</appSettings>
...
</configuration>
And everytime the app needs to be published/deployed to a certain location, the other two are commented out. So if I build a DEV-release, I comment out the PROD and TEST sections. When publishing for production, ... you get the idea.
This is a useless time-consuming work. My question is: is there a way to avoid this? A way that I still have the separate deployment-specific configuration, but that I don't need to comment (out) the needed sections.
I read many topics related to configuration manager but could not resolve the issue. I just want to read the connection string as well some appsetting keys from a CLASS LIBRARY in the web application.
I have reference to System.Configuration Class.
This is my code:
using System.Configuration;
...
string constr = ConfigurationManager.ConnectionStrings["cbuddydb"].ConnectionString;
string strUserName = ConfigurationManager.AppSettings["username"];
string strPwd = ConfigurationManager.AppSettings["password"];
But it seems reading from a different config file. not from the web.config in my project. Because the value read is wrong.
My web.config is below:
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.data>
<connectionStrings>
<clear />
<add name="cbuddydb" connectionstring=
"Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=myDataBase;Persist Security Info=True;
User=#username;Password=#password;Option=3" providerName="MySql.Data.MySqlClient" password=""/>
</connectionStrings>
<appSettings >
<clear />
<add key="username" value ="6/0RUNnSmUBsbdNoCg+9Sw=="/>
<add key="password" value =""/>
</appSettings>
</system.data>
</configuration>
The reason for this is because of configuration file inheritance. The connection string at index 0 may not be in your config file, but it may have been inherited from machine.config etc. Have a look at ASP.Net config file hierarchy and inheritance: http://msdn.microsoft.com/en-us/library/ms178685.aspx
You could clear the inherited connection strings by specifying the following in your web.config
<connectionStrings>
<clear />
<add name=”MyConnString” connectionString=“Whatever“ />
</connectionStrings>
EDIT: In your config, place your connectionStrings and appSettings tags directly below the configuration element. They should not be within the system.data element. They are direct children of the configuration element. And remove the extra password attribute after the providerName. I can't validate your connection string, since I don't know how you're using it.
<configuration>
<connectionStrings>
<clear />
<add name="cbuddydb" connectionString=
"Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=myDataBase;Persist Security Info=True;
User=#username;Password=#password;Option=3" providerName="MySql.Data.MySqlClient"/>
</connectionStrings>
<appSettings >
<add key="username" value ="6/0RUNnSmUBsbdNoCg+9Sw=="/>
<add key="password" value =""/>
</appSettings>
<system.data>
....
You should consider encrypting sensitive information in your config file, like passwords.