Web.config is very important to ASP.NET programmers, knowing its capabilities is critical to building robust applications. The post discusses the basic structure of the file and how to use the read / write methods in it.
The Web.Config File
ASP.NET has many features that the developer may use in their solutions, from assorted authentication methods to performance monitoring and scalability. Although these are quite comprehensive features with many concepts and techniques, it is very simple to interact with them, configuring them or even creating their own resources. To do this, we use Web.config.
It is not required that you add this file to your project. Inside the .NET framework folder, in Windows / System32, there is a file called machine.config that defines the default settings. Implement Web.config only if you want your application to assume specific behaviors, or if you need to save any necessary value in your project.
Web.config is an orientation for a particular ASP.NET application to be configured. Make no mistake, the .config extension is just a nomenclature, this is just an XML file that can be changed in any text editor, if you know the tags to be used.
Below you can see how is the structure and the tags of a web.config (xml) file:
<?xml version="1.0"?> <configuration> <appSettings/> <connectionStrings/> <system.web> <compilation debug="false" /> <authentication mode="Windows" /> </system.web> </configuration>
There are several points to watch. The main feature is the XML structure. Based on your ability to see and differentiate tags, let’s analyze each line and see what your goal is.
<? xml version = “1.0”?> – Responsible for file standardization, the version attribute specifies the version of the xml used.
<configuration> – Root of the web.config file. All settings must be within this tag.
<appSettings /> – Tag that serves as a container for the settings specific to your application, in this case, the settings that the developer wants to store. We will see her in more detail later.
<connectionStrings /> – Tag responsible for saving the connectionstrings (connection parameters to the database) used in your application.
<system.web> – Saves the guidelines to be used by .NET.
<compilation debug = “false” /> – Tag responsible for setting ASP.NET application build settings. The debug attribute specifies whether the debugging options are active.
Recommendation: when developing, it is common for your IDE to set this option to true, so you can use all debugging features, but it is highly recommended that when you distribute your application, put the debug attribute false, this will make your faster because you do not have to load those resources.
<authentication mode = “Windows” /> – Use this tag to define the authentication setting.
Some should be surprised that some tags are opened and then closed after their content (<> </>), others have only the opening followed by a slash (</>). It is a very simple concept. In Web.config we use the attributes as container for the settings, that is, we open and close only when they have child tags, otherwise we use the / terminator to close it.
Inside the .NET code, to access the values of Web.config, we should basically use the namespace below:
System.Web.Configuration – Namespace that holds classes that implement attributes and methods for interacting with Web.config.
To access web.config values, we usually use the ConfigurationManager object. Using this object we can access the attributes of the Web.Config. The most basic ones to be accessed are AppSettings and connectionStrings, because they are implemented as attributes of the ConfigurationManager object, in the case of others, you must use the getsection() method.