Web configuration files are important for all the ASP.net hosted websites. Every application requires some kind of settings that tells the application how should it behave. Also, it comes handy when we use as all the settings go in one place and if somebody wants to change some settings they know this one stop place. What if we don’t use these configuration files? Well. You should do it through the code. Imagine the situation of 100 settings, if it is done in the code and anyone out of that 100 requires configuration change, first somebody need to search and pin point the piece of code that requires the changes, then they should build (Web Site Down) and re-launch the websites (Web Site Up). Configuration files avoid rebuilding the websites assemblies.
2. Machine Level Configuration
Dot.Net supplies some default configuration for the essential configuration that is actually required by the application. Where from these settings coming from? Say for example even when I don’t specify a connection string the default is set to SQL Server of the local machine. Who is supplying that? Web.Config file is the answer if the connection string is specified there. What If it is not there? OK, the dot Net framework supplies two parent configuration files and some machine level general settings can be specified there. Our application actually overrides those defaults when required.
The below picture shows the default configuration files:
The screen shot shows two different configuration files. One is Machine.config and the other one is web.config. They both act as a parent for other application specific configuration files. Also, note the path. The path tells you that these common configuration files are for Dot Net Framework version 2.0 (One that come with Vs2005 IDE). You will have the same set of configuration files for other versions of the framework also.
OK. Why two different files here? The Web.Config file is for all the Web applications and machine.config file is for all the dot net applications machine wide. So if my application is not a web application, only machine.config file comes into the picture. In the mean time, if my application is web based, both machine.config and web.config comes into the picture. Modifying these files requires administrator privilege.
3. Multiple Config files & Order
Most of the web project will be organised in different folders. We can visualise that like root folder and sub-folders. And each sub-folder may have their web.config file and this is how we will come across multiple web configuration files. Consider the below-shown picture:
The C-SharpCorner.com is the root folder and it has two sub-folders namely DBCorner and VBCorner. Now the site has three homes and those can be accessed like:
The Links won’t work except first one, as this is just an example for explanation purpose. Assume that each link displays their own home page and has three different settings. So we need the web pages from three different groups and these groups have independent settings. That why we have three different web.config files on each folder.
Let us say a page inside the VBCorner sub-folder is loading. Now the configuration settings are read by the page in the below-specified order:
1) Machine.config file from the window\DotNetFramework Folder (See Picture 1)
2) Web.Config from the same folder (Only for the web application)
3) Web.Config from the D:\C-AskCorner.com folder
4) Web.config from the DbCorner folder
5) Web.Config from the VBCorner folder
Note that the Latest setting which is available in the vbcorner web.config file be in effect when the same setting is specified in all the configuration files. Also, be aware that the parent should say it is Okay to override my settings.
4. About the Sample
The sample is Simple Link pages. The main page has 4 hyperlinks and last one among them is intentionally broken. First, we will look at the usage of the configuration file and then walk through learning how do we turn on the tracing and debugging. Finally, we will see how do we set the default error page. The default error page is one, which is displayed when ASP.Net throws an error page. Below screen shot shows the link page:
When the Last broken link is clicked, the below-shown system error page is displayed. We are going to replace that error page with our custom error page.
5. Preparing the sample
Creating the sample is simple as it has only 4 link buttons. In this, we had first three hyperlinks has valid linked pages and last one refers a page that is not part of the project (Intentionally Broken) to demonstrate the usage of Default Error Page. Below video shows defining the Error Page.
1) A Web Form (ErrorPage) is added to the Project
2) Then A Label is placed in the Web Form
3) Error text is typed in the label and some properties are set for the label.
And next video here shows setting the properties for the HyperLink control.
1) Selects already placed hyperlink control and invokes properties for it
2) Along with other properties sets the hyperlink property
6. Enable Debugging
By default the debugging feature is disabled in ASP.net. When you press F5/Chose Start Debugging from debug menu, a dialog as shown below appears:
When you click ok to this dialog, the debugging feature is enabled for the solution. It makes an entry in the web.config file mentioning that web application can allow debugging. Web.Config is shown below (Portion of it):
When we deploy the website we should make sure that the debugging feature is turned off. Because this will slow down the performance of the website and this is why the debugging is turned off by default. Below video shows turning on debugging web application:
1. Start Debugging is clicked from the debug menu
2. Debug produces not enabled dialog displayed and asking for confirmation to turn it on. Clicked on the dialog
3. In the web, configuration debug switch is set to true
7. Turn-on Tracing
Just like debugging we cab turn on the tracing also. Tracing is a process, which will log the useful information and displays it when required as the information is persisted. This is useful for getting quick information without debugging the application. We can turn on the tracing feature using the ASP.Net Configuration tool.
The below picture shows how do we turn on this feature. Highlighted ones are the setting we changed from its default. We configured to Capture 15 trace requests on individual pages.
The tracing information configured by the tool is also viewable in the web.config file. The tracing turned on by the tool is shown below:
The videos given below shows how do we turn ON the trace feature and Capture Trace information:
1) Invokes ASP.Net Configuration utility from the explorer menu
2) From the Application tab, under the debugging and tracing, Configure Debugging and Tracing link are clicked
3) From the displayed page the tracing requirements are set
1) Opens the home page (The Page with all 4 links)
2) From the context menu, the page was browsed through Internet explorer
3) The captured trace information is shown
8. Defining Custom Error Page
Custom Error page is used to redirect the user to show error message defined by us. The system error message is not useful for the end users browsing the page and in such situations we display our own pages. In the attached sample solution clicking the Link Broken link as shown below generates the systems error. The system generated error information also shown in the below screenshot.
Broken link on the Home Page
System supplied Error Message
We can use the ASP.Net Configuration utility to define the default custom error pages. Once it is specified, we do have an entry for the custom error page in the web configuration file and the entry says which page to navigate when an error occurs.
Below specified videos show how do we configure the default error page and generates an error to show the re-directed page.
1) Navigate to the ASP.Net Configuration utility
2) From the displayed tool, application tab is selected
3) Then from the displayed page, define default Error page link is clicked
4) From the displayed page, default error page is defined
5) The custom error page entry is shown in the config file.
6) Finally, the mode attribute is turned on by editing (The tool turned it off by default) the web.config file.
1) Using the Google Chrome web browser, the home page is viewed from the solution explorer
2) From the displayed home page the broken web link is clicked
3) In place of showing the system error message (One shown above picture) we see a default error page