Read appSettings values from web.config in ASP.NET (C#)
The <appSettings> section holds simple key-value settings: a page size, a feature switch, an e-mail address. Reading them is one line; turning the strings into typed values with sensible defaults is where a small helper pays off.
The settings
<configuration>
<appSettings>
<add key="PageSize" value="25" />
<add key="EnableNewCheckout" value="true" />
<add key="AdminEmails" value="ops@example.org;billing@example.org" />
<add key="SupportPhone" value="" />
</appSettings>
</configuration>
Reading them
using System.Configuration; // add a reference to System.Configuration
string raw = ConfigurationManager.AppSettings["PageSize"]; // "25", or null if the key is missing
A typed wrapper keeps conversions and defaults in one place:
public static class AppConfig
{
public static int PageSize => GetInt("PageSize", 20, min: 5, max: 200);
public static bool EnableNewCheckout => GetBool("EnableNewCheckout", false);
public static string[] AdminEmails =>
(ConfigurationManager.AppSettings["AdminEmails"] ?? "")
.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
.Select(s => s.Trim()).ToArray();
private static int GetInt(string key, int fallback, int min, int max) =>
int.TryParse(ConfigurationManager.AppSettings[key], NumberStyles.Integer, CultureInfo.InvariantCulture, out int v)
&& v >= min && v <= max ? v : fallback;
private static bool GetBool(string key, bool fallback) =>
bool.TryParse(ConfigurationManager.AppSettings[key], out bool v) ? v : fallback;
}
// usage
gvOrders.PageSize = AppConfig.PageSize;
if (AppConfig.EnableNewCheckout) { /* ... */ }
Parse numbers with the invariant culture so that a server set to a culture with different formats reads the file the same way.
In markup
<asp:Literal runat="server" Mode="Encode" Text="<%$ AppSettings:SupportPhone %>" />
The <%$ … %> expression builder only works in server control property values, not as free text in the page.
Changing a value on a live site
Saving web.config restarts the application domain: in-process session state is lost and the first request after the change is slower. Plan changes accordingly, or move frequently changed settings to a database table.
Secrets
An API key in appSettings ends up in source control unless you prevent it. Use the file attribute to merge values from a file that is not committed:
<appSettings file="App_Data\secrets.config">
<add key="PageSize" value="25" />
</appSettings>
<!-- App_Data\secrets.config (not in source control) -->
<appSettings>
<add key="PaymentApiKey" value="..." />
</appSettings>
If the external file is missing, ASP.NET simply ignores it, so the application still starts in development. The section can also be encrypted with aspnet_regiis, as explained in appSettings vs connectionStrings.
Common mistakes
- Keys are case-insensitive when read, but a misspelled key returns
nullsilently. Centralise the names in one class, as above, so a typo appears in one place only. bool.TryParseaccepts "true"/"false" in any case, but not "1", "yes" or "on". Document the expected values next to the key.- Reading settings in a static field initialiser caches them for the life of the application domain; that is usually fine, since changing
web.configrestarts the domain anyway.
Official documentation
- Microsoft Learn: ConfigurationManager.AppSettings, <appSettings> element