Read Variable from Web.Config
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In ASP.NET applications, the web.config file is the central place to store configuration values like database connection strings, API keys, and feature flags. Reading these values correctly matters because hardcoding settings into your source code makes deployments painful -- you would need to recompile every time a database server changes or an API key rotates. The web.config approach lets you change behavior without touching a single line of code.
Understanding Web.Config Structure
A web.config file is an XML document with several predefined sections. The two you will use most often are appSettings for simple key-value pairs and connectionStrings for database connections.
Each appSettings entry is a key-value pair accessed by its key attribute. Connection strings are accessed by their name attribute and carry additional metadata like the database provider.
Reading AppSettings Values
The ConfigurationManager class in the System.Configuration namespace is the standard way to read from appSettings. The reason this class exists as a dedicated API (rather than parsing XML yourself) is that the framework handles caching, type safety, and environment-specific overrides for you.
If the key does not exist, AppSettings["MissingKey"] returns null rather than throwing an exception. Always check for null before parsing to avoid runtime crashes.
Reading Connection Strings
Connection strings have their own dedicated section because they carry more structure than a simple key-value pair. The ConnectionStrings property returns a ConnectionStringSettings object that includes both the connection string itself and the provider name.
This separation of connection string from provider name lets you switch between SQL Server, MySQL, and PostgreSQL by changing the config file alone.
Custom Configuration Sections
For more complex settings that do not fit neatly into key-value pairs, you can define custom configuration sections. This involves creating a class that inherits from ConfigurationSection.
Reading Configuration in ASP.NET Core
ASP.NET Core replaces web.config and ConfigurationManager with a new configuration system built on appsettings.json and the IConfiguration interface. If you are working with ASP.NET Core, this is the approach you should use instead.
The IConfiguration system supports multiple sources (JSON files, environment variables, command-line arguments) and merges them with a clear precedence order. Environment variables override file values, which is essential for containerized deployments.
You can also bind entire sections to strongly typed classes:
Common Pitfalls
- Missing the System.Configuration reference: In .NET Framework projects, you must add a reference to
System.Configuration.dll. Theusingstatement alone is not enough -- the project will compile butConfigurationManagerwill not resolve. - Assuming AppSettings returns a typed value: All
AppSettingsvalues are strings. Forgetting to parse them toint,bool, or other types causes subtle bugs when you compare or calculate with the raw string. - Not handling missing keys:
AppSettings["NonExistent"]returnsnullsilently. Without a null check, the error only surfaces later as aNullReferenceExceptionfar from the actual problem. - Confusing .NET Framework and .NET Core configuration:
ConfigurationManagerdoes not exist in ASP.NET Core by default. Trying to use it in a Core project without the compatibility NuGet package leads to confusing compile errors. - Storing secrets in web.config committed to source control: Connection strings and API keys in
web.configend up in your Git history. Use user secrets (development) or environment variables (production) for sensitive values.
Summary
- Use
ConfigurationManager.AppSettings["key"]to read simple key-value settings from theappSettingssection ofweb.config. - Use
ConfigurationManager.ConnectionStrings["name"]for database connection strings, which include both the connection string and provider name. - All
AppSettingsvalues are strings -- always parse them to the correct type and check fornullon missing keys. - Custom configuration sections let you organize complex settings into strongly typed classes.
- In ASP.NET Core, replace
web.configwithappsettings.jsonand injectIConfigurationthrough dependency injection. - Never store secrets directly in configuration files that are committed to source control.
Related reading
- ReaderWriterLock vs lock
- ReaderWriterLock vs lock
- Reading a C/C data structure in C from a byte array
- Reading Excel files from C
- Reading large text files with streams in C
- Reading PDF documents in .Net
- Reading settings from app.config or web.config in .NET
- Reading settings from app.config or web.config in .NET

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.