Trouble with web.config - c#

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

Related

Can't read connection string from web.config

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"

Web.Debug.config vs. Web.Release.config running web app in localhost

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

Get winform Webbrowsercontrol Source from app config file

Can someone please show me how to get winforms Webbrowsercontrol source from appconfig file ?.
I tried with following but it's not working
webBrowser2.Navigate = ConfigurationManager.AppSettings["dateandtime"];
and this is my how app config file look like
<?xml version="1.0"?>
<configuration>
<configSections>
</configSections>
<appSettings>
<add key="serverip" value="127.0.0.1" />
<add key="dbport" value="3306" />
<add key="defdatabase" value="waq115" />
<add key="dateandtime" value="http://free.timeanddate.com/clock/i4daxch9/n77/fs18/fcfff/tc212426/pc212426" />
</appSettings>
<startup><supportedRuntime version="v2.0.50727"/></startup>
</configuration>
Navigate() is a method not a variable, So the following line of code will not compile,
webBrowser2.Navigate = ConfigurationManager.AppSettings["dateandtime"];
You have to read the URL from the application configuration file and pass it as an argument to Navigate("URL") method as follows.
webBrowser1.Navigate(ConfigurationManager.AppSettings["dateandtime"]);

Transform config in asp.net mvc isnt updated

I have asp.net mvc 4 project where try to use transform config, where have some default values and when I try to change to release mode some default values change to release values, but it isnt work. When I try to change to release mode and build application I have nothing changes. Here is my default values in Web.config:
<appSettings>
<add key="appId" value="####"/>
<add key="appSecret" value="####"/>
<add key="hostName" value="####"/>
</appSettings>
And here is what I have in my Web.Release.config:
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="appId" value="!!!!" xdt:Transform="Replace" xdt:Locator="Match(name)" />
<add key="appSecret" value="!!!!" xdt:Transform="Replace" xdt:Locator="Match(name)"/>
<add key="hostName" value="!!!!" xdt:Transform="Replace" xdt:Locator="Match(name)"/>
</appSettings>
</configuration>
Does anybody help me?
Using xdt:Locator="Match(name)", you're trying to match an attribute name which does not exist. You're looking for key for the appSettings:
<add key="appId" value="!!!!" xdt:Transform="Replace" xdt:Locator="Match(key)" />
<add key="appSecret" value="!!!!" xdt:Transform="Replace" xdt:Locator="Match(key)"/>
<add key="hostName" value="!!!!" xdt:Transform="Replace" xdt:Locator="Match(key)"/>

Is there anything wrong with ConfigurationManager in C#?

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.

Categories