.NET  

Web.config vs App.config — Key Differences & Usage in .NET

In .NET applications, configuration files play a vital role in defining settings like database connections, security rules, and application behavior. Two primary configuration files used by developers are:

  • Web.config (for ASP.NET Web applications)

  • App.config (for Windows/Console/WPF applications)

Although they serve similar purposes, they are used in different environments and work differently.

What is Web.config?

Web.config is a configuration file used only in ASP.NET web applications.

Key Purposes

  • Database connection strings

  • Security settings & authentication

  • Sessions & caching configuration

  • Error handling (custom errors)

  • URL routing rules

Example Web.config

<configuration>
  <connectionStrings>
    <add name="dbConn" 
         connectionString="Data Source=SQLSERVER;Initial Catalog=MyDB;User ID=sa;Password=123" 
         providerName="System.Data.SqlClient" />
  </connectionStrings>

  <system.web>
    <authentication mode="Forms" />
    <customErrors mode="On" />
    <sessionState timeout="20"/>
  </system.web>
</configuration>

What is App.config?

App.config is used in Desktop, Console, WPF, Windows services, and class library projects.

Key Purposes:

  • Database or API configuration

  • Application-level settings (UI themes, logs, intervals)

  • Third-party library configuration

Example App.config

<configuration>
  <appSettings>
    <add key="ApiUrl" value="https://api.myapp.com"/>
    <add key="EmailSupport" value="[email protected]"/>
  </appSettings>

  <connectionStrings>
    <add name="dbConn" 
         connectionString="Data Source=SQLSERVER;Initial Catalog=MyDB;Integrated Security=True" />
  </connectionStrings>
</configuration>

Real-Time Scenario Comparison

Use CaseWeb.config (Web App)App.config (Desktop App)
Employee portalUsedNo
Billing system installed in office PCsNoUsed
Web API service configurationUsedNo
Windows Background ServiceNoUsed

Key Differences

FeatureWeb.configApp.config
PlatformASP.NET Web applicationsDesktop / Console / WPF
ExecutionManaged by IIS / Web ServerCompiled into .exe.config
Supports multiple config filesYes (Web.Release.config, Web.Debug.config)No (single config)
Request/Response settingsAvailableNot applicable
URL rewriting & securityAvailableNot applicable

How the files compile?

Original FileCompiled Output
Web.configRemains Web.config in deployed website
App.configRenamed to MyApp.exe.config

Real-World Example

E-commerce Website (Web.config)

  • Connection strings

  • Session timeout

  • Payment gateway API keys

  • Custom error pages (404, login error)

Inventory Management Desktop App (App.config)

  • Database server location

  • Offline mode settings

  • Sync intervals

  • Printer configurations

Conclusion

Web.configApp.config
Used in Web appsUsed in Desktop/Console apps
Supports web-specific featuresGeneral application settings
Not compiled into exeCompiled into .exe.config

Both files are essential but serve different environments and configurations.
Using them correctly ensures secure, scalable, and maintainable applications.

Tip for Developers

Never hard-code credentials.
Use configuration files + encryption for sensitive data.