Can I split appSettings into multiple external config files and include them in main web.config?
Here is what I have tried, but its not working :(
In web.config I defined a new section:
<configSections>
<section name="ssoConfiguration" type="System.Configuration.NameValueSectionHandler"/>
</configSections>
below I have this section:
<ssoConfiguration>
<add key="SSOEnabled" value="true"/>
</ssoConfiguration>
When I call System.Configuration.ConfigurationManager.AppSettings["SSOEnabled"] it returns null.
Any ideas why?
Also, I will have multiple sections with such appSettins - is it possible to define them in multiple external config files and get them included in main web.config?
Thank you.
Because you're accessing AppSettings, but the "SSOEnabled" setting is in another section ("ssoConfiguration"). Try
var ssoConfiguration = (NameValueCollection)ConfigurationManager.GetSection("ssoConfiguration")
var ssoEnabled = ssoConfiguration["SSOEnabled"];
Related
There are several answered questions detailing how to refer to a single value from web.config file. I'd like to maintain lists of values with which to populate combo boxes in the Views. Should I use some sort of key/value pair structure, with the key identifying the particular combo box to populate?
<add key="CalculationMethod" value="Fixed"/>
<add key="CalculationMethod" value="Cost Plus"/>
<add key="CalculationMethod" value="Formula"/>
I'm not sure how I'd read this, or if that would even work (don't keys have to be unique?). The IntelliSense for web.config doesn't seem to allow much in the appSettings section that looks applicable to a more robust structure like
<list name="CalculationMethod">
<item value="Fixed"/>
<item value="Cost Plus"/>
<item value="Formula"/>
</list>
I see that the root <configuration> has a lot of options for children, but which one to use, if any?
You really should be using a database for something like this but if you must put it in the web.config then you could delimit the values and parse them out at run time.
<add key="CalculationMethod" value="Fixed,Cost Plus,Formula"/>
myComboBox.DataSource = new List<String>(lstrCalcMethodsFromWebConfig.Split(','));
I tried to add this value but it is giving me the error
<add key="RssFeedURL" value="http://www.example.com/?cat=11&feed=rss2"/>
You could encode it:
<add key="RssFeedURL" value="http://www.example.com/?cat=11&feed=rss2"/>
I'm working on a ASP.NET MVC3 web application (not written by me) which has a max upload size of 100MB. Now this web application gets installed on server machines for customers so it would be nice if this value max upload size was configurable for each customer. They have access to edit the web.config for the web application if need be.
Now there's a value in the web.config like so:
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="104857600" />
</requestFiltering>
</security>
</system.webServer>
Also there's another value here which appears to be similar:
<system.web>
<httpRuntime maxRequestLength="104857600" executionTimeout="360" />
</system.web>
That 104857600 bytes appears to be the 100MB file upload limit. However on changing the value I discovered that this isn't the authoritative value and it wasn't obeying the new limit. So after some more digging I found somewhere else in the C# code was a hardcoded value public const double MaxContentSize = 104857600 and another C# method was using that value to accept/deny the Ajax file upload.
So what I think I'd like to do is replace that hard coded number in the code so it reads from the value in the web.config. Then at least anyone can change that value in the web.config when they deploy the website.
Can you do something like this?
MaxContentSize = ConfigurationManager.systemWeb.httpRuntime['maxRequestLength'];
I've seen some examples using appSettings in the web.config e.g.
<appSettings><add key="MySetting" value="104857600" /></appSettings>
then accessing it like:
ConfigurationManager.AppSettings["MySetting"]
But that would mean adding a custom value in there and now we'd have 3 places to change it in the web.config. Anyone got an idea how to do it properly?
Many thanks
You can do something like:
int maxRequestLength = 0;
HttpRuntimeSection section =
ConfigurationManager.GetSection("system.web/httpRuntime") as HttpRuntimeSection;
if (section != null)
maxRequestLength = section.MaxRequestLength;
There seems to be no easy way to read the system.webServer section, because it is marked as "ignored" from machine.config.
One way is to parse the XML of the web.config file directly:
var config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
var section = config.GetSection("system.webServer");
var xml = section.SectionInformation.GetRawXml();
var doc = XDocument.Parse(xml);
var element = doc.Root.Element("security").Element("requestFiltering").Element("requestLimits");
string value = element.Attribute("maxAllowedContentLength").Value;
Try:
var config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/")
var section = (System.Web.Configuration.SystemWebSectionGroup)config.GetSectionGroup("system.web")
var maxRequestLength = section.HttpRuntime.MaxRequestLength
I have my appSettings defined in a separate config file called Appsettings.Dev.Config, and I include that file inside my web.config file like so
<appSettings configSource="ConfigFiles\AppSettings.Dev.config"/>
Lets say one of the settings in the file is
<add key="MailerEmailAccount" value="myemail#myserver.com" />
Can I access the value of the setting MailerEmailAccount elsewhere inside web.config? How?
Nope, the web configuration file cannot pull "settings" from itself; it's not dynamic at all. The only sort of dynamic functionality is the ability to include other .config, but that's just a "suck all these settings in as if they were part of me" kind of thing.
It might be possible if you create a custom ConfigurationSection that pulls the value from appSettings.
Here's an article that explain how to create a custom configuration section:
http://haacked.com/archive/2007/03/12/custom-configuration-sections-in-3-easy-steps.aspx
I don't know if this is what you're looking for, but it's the only way I can think of to read a web.config setting from within the web.config.
EDIT
I haven't tested this, but maybe something like this would work?:
[ConfigurationProperty("localName", IsRequired = true, IsKey = true)]
public string LocalName
{
get
{
return this["localName"] as string;
}
set
{
this["localName"] = WebConfigurationManager.AppSettings.Get(value);
}
}
I have this code in my Web.Config file:
<configSections>
<section name="myWebAppSettings" type="System.Configuration.SingleTagSectionHandler" />
</configSections>
<myWebAppSettings isTestEnvironment="true"/>
I need retrieve my value isTestEviroment from Global.asax
At the moment I use with no sucess:
bool isTestEnvironment = ConfigurationManager.AppSettings.GetValues["isTestEnvironment"];
What I'm doing wrong here?
NOTES: I do not assume my Web.Config file is right so please feel free to change it if I did not written correctly. Thanks for your help on this!
ConfigurationManager.AppSettings retrieves values from the AppSettings configuration element, not your custom section.
You need to use:
var section = (HashTable)ConfigurationManager.GetSection("myWebAppSettings");
bool isTest = Boolean.Parse(section["isTestEnvironment"].ToString());