Free Web Development Online Tutorials, Learn to Code
Learn Asp.net Aspx, Free Asp.net Tutorial in C#, Asp.net Web Application Development Guide

How to use Web.config file in Asp.net

Web.Config in Asp.net Application is a human-readable XML file, that you can open and edit with any simple text editor, Web.Config file contain configuration information of asp.net application, when you create a asp.net application a default Web.Config will be added in root of your application.

Using Web.Config in Asp.net

Web.Config contain configuration information for a particular asp.net Application, but in hierarchy there another file called Web.Config, that contain configuration information of all asp.net application on that machine.

\WINNT\Microsoft.NET\Framework\[Framework Version]\CONFIG\machine.config

To make any changes in that file you need to have system level permission.

Now we talk about only Web.Config, while developing application the file will reside in root of your app, and while deploying on IIS, the file will be at the root of virtual directory.

wwwroot\Web.Config

Whenever you make any changes in Web.Config settings the application or the service will be automatically restarted, even a single letter changes will take the effect.

The top tag in web.config is configuration

<configuration>
    ...
</configuration>

Here we discuss some of the important tags, which you change very often in your application

appSettings in web.config

You can add any number of key-value pair in this section

<appSettings>
<add key="webpages:Version" value="2.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="PreserveLoginUrl" value="true" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="TestKey" value="TestValue"/>
  </appSettings>

appSettings is the section where you, you can add a new key-value, then retrieve the same in your application using ConfigurationManager.AppSettings

System.Configuration.ConfigurationManager.AppSettings["TestKey"];

customErrors in web.config

If you want to implement custom error page to display user-friendly messages for any unhandled application error, then you can specify different page for different error code.

<customErrors mode="Off" defaultRedirect="~/error">
<error statusCode="404" redirect="~/error/page404" />
<error statusCode="500" redirect="~/error/page500" />
</customErrors>

Custom configuration details in web.config

In this section of web.config you can add all Custom configuration details, please read the complete article on how to implement configuration framework in asp.net application

<sectionGroup name="CustomSetting">
     <section name="SystemSettings" type="SystemHandler,WebTrainingRoom" />
     <section name="BusinessSettings" type="BusinessHandler, WebTrainingRoom" />
    </sectionGroup>
            

Hope now you got the idea of how to work with web.config in asp.net application

Comment
Name
Email
Website
Subscribe
 
Web.config in Asp.net with appSettings, configuration, customErrors examples