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());
Related
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"];
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);
}
}
so I'm very very far from an expert on MVC or ASP.NET. I just want to make a few simple Controllers in C# at the moment, so I have the following question;
Right now I have the connection string used by the controller, -inside- the controller itself. Which is kind of silly when there are multiple controllers using the same string. I'd like to be able to change the connection string in just one place and have it affect all controllers.
Not knowing a lot about asp.net or the 'm' and 'v' part of MVC, what would be the best (and simplest) way of going about accomplishing just this?
I'd appreciate any input on this, examples would be great too.
Put it in your web.config file like so:
<connectionStrings>
<add name="ConnectionName" connectionString="TheConnectionString" providerName="System.Data.SqlClient" />
</connectionStrings>
<connectionStrings> is just a child of the root <configuration> element.
Then you can read it like:
string myConnStr = ConfigurationManager.ConnectionStrings["ConnectionName"].ConnectionString;
HTHs,
Charles
You may add a connection string to the web.config file like this:
<configuration>
<appSettings>
<add key="ConnectionString"
value="server=localhost;database=Northwind;uid=sa;password=secret;"/>
</appSettings>
<system.web>
<customErrors mode="Off"/>
</system.web>
</configuration>
and use it in your code like this:
strConnection = ConfigurationSettings.AppSettings("ConnectionString") // <-----
sqlConn = New SqlConnection(strConnection)
sqlCmd = New SqlCommand("SELECT * FROM Customers WHERE " & "(CompanyName LIKE 'A%') OR (CompanyName LIKE 'B%')", sqlConn)
Notice the first line. This example is for VB but you should be able to do the same in C#.
The example is taken from this link http://www.dotnetjohn.com/articles.aspx?articleid=3, but there are tons of them everywhere.
Hope this helps.
I have a control containing some text which I want to get from a resx file, I thought I could just create a file called ControlName.ascx.resx but that doesn't seem to be working.
I'm using
label1.InnerText = (string)GetLocalResourceObject("default");
To get the value from the resource file but it keeps throwing up an InvalidOperation Exception.
Am I right about how resx files work or does it only work for Pages?
I have the same code working on an aspx page.
When you call GetLocalResourceObject from within a user control, you are actually calling TemplateControl.GetLocalResourceObject, which will look in the wrong place for the resource file. You need to call HttpContext.GetLocalResourceObject instead.
protected string HttpContextGetLocalResourceObjectAsString(string message)
{
string path = HttpContext.Current.Request.Path;
return (HttpContext.GetLocalResourceObject(path, message) as string);
}
Now you can do
label1.InnerText = HttpContextGetLocalResourceObjectAsString("default");
Per the documentation:
Gets a page-level resource
http://msdn.microsoft.com/en-us/library/system.web.httpcontext.getlocalresourceobject.aspx
Edit- added
It may be less work to just add the string to the web.config and grab it from there.
<configuration>
<appSettings>
<add key="LoggingSystemId" value="B2F085A9-6EC1-4CBF-AF8B-B17BFA75AD81"/>
<appSettings>
...
referenced as follows:
logger.SystemId = System.Configuration.ConfigurationManager.AppSettings["LoggingSystemId"];
Of course, you'll need a reference to the System.Configuration dll.
A year or so later, but i think this is what you're after?
var resource = HttpContext.GetLocalResourceObject(TemplateControl.AppRelativeVirtualPath, termType.ToString());
Mark answer if that's the one!