Asp.net web.config example

Web config is one of the most important file that contain configuration information in any asp.net application, here we learn what is web.config file in asp.net application and how to use that file!

What is Web.Config 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

web.config appSettings example

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"];
web.config customErrors example

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>
web.config Custom configuration example

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

 
Hire Asp.Net Developer
Asp.net Web.config appSettings configuration customErrors examples
Asp.net Interview Questions Answers
Asp.net MVC interview
Asp.net Core question
Asp.net application development tutorials with real-time examples create asp.net web application with SQL database step-by-step.
Asp.Net C# Examples | Join Asp.Net Course | Learn Asp.net MVC | Asp.net Core Tutorial