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!
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.
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
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"];
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>
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