Getting Null while reading my appsettings in MVC - c#

Here is my appsettings
<appSettings>
<add key="webpages:Enabled" value="true" />
<add key="K1" value="Debendra Dash"/>
</appSettings>
Here is how i am trying to read in my controller:
string x = System.Configuration.ConfigurationManager.AppSettings["K1"];
And am getting null always.
Here is my web.config.

Your code is correct.
Try a couple of things
See if you're editing the correct Web.config.
Try using WebConfigurationManager instead of ConfigurationManager

Related

Saving existing file: empty value attribute is removed

I have a config file like this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="key1" value="value1" />
<add key="key2" value="" />
...
</appSettings>
...
</configuration>
I read the config, change the value of key1 and save the config
System.Configuration.Configuration appConfig = ConfigurationManager.OpenExeConfiguration(Configuration.ConfigFile.Replace(".config", string.Empty));
appConfig.AppSettings.Settings["key1].Value = "newvalue1";
appConfig.Save(ConfigurationSaveMode.Minimal);
After this I get the following result:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="key1" value="newvalue1" />
<add key="key2"/>
...
</appSettings>
...
</configuration>
Why is the 'empty' value attribute removed for key "key2"?
When I try it with
appConfig.Save(ConfigurationSaveMode.Modified);
, the attribute is not removed. But I want to know why it's removed in the first case?
Thanks
<add key="key2" value="" />
and
<add key="key2" />
are both functionally equal. The value of key2 with or without that tag is a null string variable which is equivalent to the empty string.
Edit to reflect comment: Simply assign key2 the empty string in the same statement.
System.Configuration.Configuration appConfig = ConfigurationManager.OpenExeConfiguration(Configuration.ConfigFile.Replace(".config", string.Empty));
appConfig.AppSettings.Settings["key1"].Value = "newvalue1";
appConfig.AppSettings.Settings["key2"].Value = "";
appConfig.Save(ConfigurationSaveMode.Minimal);
Second Edit for 2nd comment:
Well, you haven't provided enough information then. All of my answers answer your question. The two values from the first answer are functionally equivalent. Therefore, if you wish to see if the "key2" value = "" then you could simply run the following
if(appConfig.AppSettings.Settings["key2"] == null){
//If this hits, that means <add key="key2" value="" />
}
Please research how to ask a good question and adjust your question to reflect what you are really trying to ask.

WebConfig Merge AppSettings

The situation is this, I want to have a key in my Web.config that gets changed in my Web.Debug.config and my Web.Release.config but I am unsure as to how to accomplish this.
I have my 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"/>
</appSettings>
I'm not sure what my Web.Debug.config is meant to be to add a key key="host" value="somevalue", so I've tried this:
<appSettings>
<add key="RunningHost" value="http://localhost:55169/" xdt:Transform="Insert" />
</appSettings>
This:
<appSettings xdt:Transform="Insert">
<add key="RunningHost" value="http://localhost:55169/" />
</appSettings>
This:
<appSettings xdt:Transform="Replace">
<add key="RunningHost" value="http://localhost:55169/" />
</appSettings>
To no avail. I read my config like so:
var appSetting = WebConfigurationManager.AppSettings["RunningHost"];
So I found out the answer for this.
It's because when I debugged the web project it didn't compile a new config combining the web.debug.config with the web.config.
However, if you publish it for release then it will combine the web.release.config with the web.config.
So the simple fix was to put the RunningHost in the web.config. Then have the release override it!
The first option is correct. Your full Web.Debug.config should look something like this:
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="RunningHost" value="http://localhost:55169/" xdt:Transform="Insert" />
</appSettings>
</configuration>
You can right-click on the Web.Debug.config in Solution Explorer and choose Preview Transform to see the transformed output.

Set Queue Path in Web.config

I am using MSMQ in C#, how can i set queuepath in web.config?
Any suggestions?
Thanks
Add an appSetting key with the path of your Queque ?
<appSettings>
<add key="myquequepath" value="FormatName:DIRECT=OS:machinename\private$\MyQueue" />
</appSettings>
It was really simple i found solution.
<add key="MSMQName" value=".\private$\WebSiteEmails"/>

Get connectionstring from web.config

I use System.Configuration.ConfigurationManager.AppSettings["key1"] in settings.designer.cs file. It's working fine in the development but after I moved all the .dll files into production it is not working.
In web.config file I added app settings in development and production both. What is the problem?
Code from settings.designer.cs file
get
{
return WebConfigurationManager.AppSettings["ConnectionString"];
//return (AppSettings["ConnectionString"]);
//return ((string)(this["ConnectionString"]));
}
I tried all three return statements. 3rd return is working fine in both dev & prod but it is not rendering from web.config.
Code in web.config
<add key="ConnectionString" value="connection string values are given here">
Don't use WebConfigurationManager.
Use System.Configuration.ConfigurationManager.AppSettings["key"] instead to read key-value pair kept in Web.config, e.g.:
<configuration>
<appSetttings>
<add key="key1" value="value1" />
</appSetttings>
</configuration>
and System.Configuration.ConfigurationManager.ConnectionStrings["name"].ConnectionString to read connection string, e.g.:
<configuration>
<connectionStrings>
<add name="name" connectionString="value1" />
</connectionStrings>
</configuration>
You have to add configuration setting (connectionstring) to last execution program config file.

How to change the value of attribute in appSettings section with Web.config transformation

Is it possible to transform the following Web.config appSettings file:
<appSettings>
<add key="developmentModeUserId" value="00297022" />
<add key="developmentMode" value="true" />
/* other settings here that should stay */
</appSettings>
into something like this:
<appSettings>
<add key="developmentMode" value="false" />
/* other settings here that should stay */
</appSettings>
So, I need to remove the key developmentModeUserId, and I need to replace the value for the key developmentMode.
You want something like:
<appSettings>
<add key="developmentModeUserId" xdt:Transform="Remove" xdt:Locator="Match(key)"/>
<add key="developmentMode" value="false" xdt:Transform="SetAttributes"
xdt:Locator="Match(key)"/>
</appSettings>
See Also: Web.config Transformation Syntax for Web Application Project Deployment
Replacing all AppSettings
This is the overkill case where you just want to replace an entire section of the web.config. In this case I will replace all AppSettings in the web.config will new settings in web.release.config. This is my baseline web.config appSettings:
<appSettings>
<add key="KeyA" value="ValA"/>
<add key="KeyB" value="ValB"/>
</appSettings>
Now in my web.release.config file, I am going to create a appSettings section except I will include the attribute xdt:Transform=”Replace” since I want to just replace the entire element. I did not have to use xdt:Locator because there is nothing to locate – I just want to wipe the slate clean and replace everything.
<appSettings xdt:Transform="Replace">
<add key="ProdKeyA" value="ProdValA"/>
<add key="ProdKeyB" value="ProdValB"/>
<add key="ProdKeyC" value="ProdValC"/>
</appSettings>
Note that in the web.release.config file my appSettings section has three keys instead of two, and the keys aren’t even the same. Now let’s look at the generated web.config file what happens when we publish:
<appSettings>
<add key="ProdKeyA" value="ProdValA"/>
<add key="ProdKeyB" value="ProdValB"/>
<add key="ProdKeyC" value="ProdValC"/>
</appSettings>
Just as we expected – the web.config appSettings were completely replaced by the values in web.release config. That was easy!
If you want to make transformation your app setting from web config file to web.Release.config,you have to do the following steps.
Let your web.config app setting file is this-
<appSettings>
<add key ="K1" value="Debendra Dash"/>
</appSettings>
Now here is the web.Release.config for the transformation.
<appSettings>
<add key="K1" value="value dynamicly from Realease"
xdt:Transform="SetAttributes"
xdt:Locator="Match(key)"
/>
</appSettings>
This will transform the value of K1 to the new value in realese Mode.
I do not like transformations to have any more info than needed. So instead of restating the keys, I simply state the condition and intention. It is much easier to see the intention when done like this, at least IMO. Also, I try and put all the xdt attributes first to indicate to the reader, these are transformations and not new things being defined.
<appSettings>
<add xdt:Locator="Condition(#key='developmentModeUserId')" xdt:Transform="Remove" />
<add xdt:Locator="Condition(#key='developmentMode')" xdt:Transform="SetAttributes"
value="false"/>
</appSettings>
In the above it is much easier to see that the first one is removing the element. The 2nd one is setting attributes. It will set/replace any attributes you define here. In this case it will simply set value to false.

Categories

Resources