X



Home>Tutorial>

ASP.Net Configuration Files:Machine.config vs Web.config vs App.config

Nuelson Penuel Saturday, August 29, 2020 ASP.Net

Summary

In this post, I will explain to you different ASP.Net configuration files such as web.config, machine.config, and app.config used during web development. Each of these configuration files can be used based on your needs.

Web.config

The web.config is a place where basic application settings such as connection strings are stored. Furthermore, It is a configuration file, which is used in web application and it can be an ASP.NET project or MVC project. Some project contains multiple web.config file inside the same project but with different folder. They have their unique benefits. You can create several web.config file in each folder with their unique benefits as per your project requirement. It is used to store the application level configuration. Sometimes it inherits the setting from the machine.config. It parses at runtime, means if you make any changes then web application will load all the settings in the config file. You don’t need to create a web.config file because it is auto generated when you create a new web application. If you want to create a web.config manually you can create it.

What Web.config file Contains?

There are number of important settings that can be stored in the configuration file. Some of the most frequently used configurations, stored conveniently inside Web.config file are: 1. Database connections 2. Caching settings 3. Session States 4. Error Handling 5. Security

How ASP.Net Process Web.config file

When you initially run your web application, the runtime builds a cache of the configuration settings for your web application by flattening the layer of configuration files as below, 1. The Machine.config file settings are retrieved. 2. The settings from the root Web.config files are added to the caches, overwriting any conflicting settings that were earlier while reading the Machine.config file. 3. If there is a Web.config file at the root of the website, this file is read into the cache, all overwriting any existing settings. The resulting cache contains the setting for this website. 4. If you have subdirectories in your web application, each subdirectory can have a Web.config file that includes settings that are specific to the files and folders that are contained within the subdirectory. To calculate the effective setting for the folders, the website settings are read and then this Web.config file is read into cache for this folder, overwriting (and thereby overriding) any existing settings.

Web.config Sample Example

    <?xml version="1.0" encoding="utf-8" ?>  
    <configuration>  
      <system.web>  
      
      </system.web>  
    </configuration>  

Machine.config

It is a special type of configuration file which creates into the OS when you install visual studio. This stores machine level configuration setting. Only one machine.config file exists into the system and it stores highest level of configuration settings. Machine.config settings apply to all web applications which is residing on the server. The setting of machine.config can be overridden by web.config’s settings. If your system does not contain the machine.config then you cannot execute the application.

Path of Machine.config

32-bit System
%windir%\Microsoft.NET\Framework\[version]\config\machine.config
64-bit System
    %windir%\Microsoft.NET\Framework64\[version]\config\machine.config

Machine.config Sample Example

    <?xml version="1.0" encoding="UTF-8" ?>  
    <!--  
        Please refer to machine.config.comments for a description and  
        the default values of each configuration section.  
      
        For a full documentation of the schema please refer to  
        http://go.microsoft.com/fwlink/?LinkId=42127  
      
        To improve performance, machine.config should contain only those  
        settings that differ from their defaults.  
    -->  
    <configuration>  
      <configSections>  
      </configSections>  
    </configuration>  

App.config

It is also a special type of configuration file which is basically used with Windows Services, Windows application, Console Apps or it can be WPF application or any others. It parses at compile time; it means if you edit the app.config when program is running, then you need to restart the application to reload the configuration setting into the program. When you run the application which contains the app.config, at the time of compilation a copy of app.config with different name moves into build folder for running the application, So that's why we need to restart the program if any changes made in app.config. It is not added auto when you create a project, to add this you need to go to solution explorer and choose Add New Item and choose “Application Configuration File”. Windows application always contains the App.config file into the project.

App.config Sample Example

    <?xml version="1.0"?>  
    <configuration>  
        <connectionStrings>     
            <add name="MyKey"   
                 connectionString="Data Source=localhost;Initial Catalog=ABC;"  
                 providerName="System.Data.SqlClient"/>  
        </connectionStrings>  
    </configuration>  
Find more information on c# corner https://www.c-sharpcorner.com/uploadfile/puranindia/Asp-Net-web-configuration-file/

| | Views(483)

See Demo | Download File


Add your comment. Indicate code using code tag


Words: 0 | Chars: 0 | Reading Duration: 0



Images Attachment




Emmason Integratded Services(2017-2024)
All Rights Reserved
Designed and Maintained By Emmason Integrated Services