1. Need for Web Config Files
Web configuration (Web Config) files are important for all the ASP.net hosted websites. Every application requires settings that tells the application how it should behave. Also, it will be handy to 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 Web Config files?
We should do it through the code. Now we will think of a situation of 100 settings for a website. If it is done in the code, anyone requires configuration change out of these 100, first need to search and pinpoint the piece of code that requires the changes. Then they should compile the binaries of the website and re-launch it. This requires a website maintenance window & during that time the website will be down. The ‘Web Config’ files avoid rebuilding the websites assemblies and keeps the website up and running.
2. Machine Config & Web Config
Dot.Net supplies default Web Config for the essential configuration required for the application. Where from these settings are coming? Say for example even when we don’t specify a connection string, the framework sets the default to SQL Server of the local machine. Who is supplying that? Web.Config file is the answer if it contains the connection string. What If it is not there? Dot Net framework supplies two parent Web Config files and machine level general settings can be specified there. Our application overrides those defaults when required.
The below picture shows the default Web Config files:
The screenshot shows two different Web Config files. One is ‘Machine.config’ and the other one is ‘web.config’. These two files are parent for other application specific configuration files. Also, note the path. The path tells us that these common configuration files are for Dot Net Framework version 2.0 (One that come with VS2005 IDE. We will have the same set of configuration files for other versions of the framework as well.
Why there are two distinct files here? The ‘Web.Config’ file is for all the web applications and ‘Machine.config’ file is for all system wide the dot net applications. So, if our application is not a web application, only ‘Machine.config’ file comes into the picture. In the meantime, if our application is web-based, both ‘Machine.config’ and ‘web.config’ are suited. Changing these files requires admin privilege.
3. Multiple Config Files & Order
The website creators will organize most of the web project in different folders. We can visualize that like root folder and sub-folders for main website and child websites. And each sub-folder may have their web.config file and this is how we will come across multiple Web Config files. Have a look at the below picture:
The C-SharpCorner.com is the root folder, and it has two sub-folders, DBCorner and VBCorner. Now the site has three homes and we can access these 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 distinct groups and these groups have independent settings. That is why we are having three different ‘web.config’ files on each folder.
Let us say a page inside the VBCorner sub-folder is loading. Now the Web Config settings are read by the page in the below-specified order:
- Machine.config file from the windows\DotNetFramework Folder (See Picture above)
- Web.Config from the same folder from the above folder
- The Web.Config from the D:\C-AskCorner.com folder
- Web.config from the DbCorner folder
- Web.Config from the VBCorner folder
Note, when the website reading the settings, it overrides the already read settings with the latest one. For Example, if page background specified in the Machine.config, the same setting present in DBCorner will replace that. This way, whatever we define deep in the tree gets precedence over the other.
4. About this Example
The sample is Simple Link pages. The main page has 4 hyperlinks and last one among them is broken. First, we will look at the usage of the Web Config file and then walk through, learning how to turn on the tracing and debugging. Finally, we will see how we set the default error page. The default error page is one, which is displayed when ASP.Net throws an error. The link page example is shown in the below Screenshot:
When the user clicks the broken link, the browser displays the below system error page. We will replace this page with our custom error page.
5. Preparing the Web Config Example
Creating the sample is simple as it has only 4 link buttons. In this, First three links are valid and the last one we keep it as broken. This is to demonstrate the custom error page. Below video shows how to defining the Error Page.
- First, we add a Web Form for Error page to the Project.
- Then, we add a Label to this Web Form.
- Finally, after typing the error text in the label, we set some properties for the label.
And next video here shows setting the properties for the HyperLink control. One can use the same for other hyper link controls. For the last Hyper link, we provide a invalid link which is not reachable.
- The video selects already placed hyperlink control and invokes properties for it.
- Then it shows what properties we set for the link controls.
6. Enable ASP.Net Debugging in Web Config File
By default, the IDE disables the debugging feature of ASP.net. When we press F5 or chose ‘Start Debugging’ from debug menu, a dialog as shown below appears:
When we click OK to this dialog, the Visual Studio IDE enables the debugging feature for the solution. It makes an entry in the ‘web.config’ file mentioning that web application can allow debugging. The below picture shows part of the ‘Web.Config’ file:
When we deploy the website, we should make sure that we turn off the debugging. Because this will slow down the website performance. However, the debugging is turned off by default. Below video shows turning on debugging for web application:
- First we click the Start Debugging from the debug menu.
- Next we click OK to the dialog to Turn-On the debugging.
- Finally, the video shows where exactly visual studio sets the switch in the Web.Config file.
7. Turn ON Debug Tracing in Web Config
Just like debugging, we can turn ON the tracing as well. Tracing is a process which will log the useful text data and displays it when needed 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 (Video is at the end of this section) how one can 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 view-able in the web.config file. The tracing turned ON by the tool is shown below:
The videos given below shows how to turn ON the trace feature and Capture Trace information:
- Invokes ASP.Net Configuration utility from the explorer menu.
- From the Application tab, under the debugging and tracing, Configure Debugging and Tracing link is clicked.
- From the displayed page, we set the tracing requirements.
- First we open the home page (The Page with all 4 links).
- Then from the context menu, we browse the page through Internet Explorer.
- Finally, we review the captured Trace Information.
8. Replace Default Error Page With Custom Error Page
The site admin uses Custom Error Page to redirect the user to it when their browsing action throws error. The system error message is not useful for the end users browsing the page. In such cases, we show our own Error page. In the attached sample solution, clicking the Broken link as shown below causes the system supplied error. The system made error page is in the below screenshot:
We can use the ASP.Net Configuration tool to set the default custom error page. Once it is stated, we have an entry for the custom error page in the Web Config file and the entry says which page to navigate when an error takes place. The below videos show how we configure the default error page and generate an error to show the re-directed Custom Error Page.
- Navigate to the ASP.Net Configuration utility.
- From the displayed tool, application tab is selected.
- Then from the displayed page, ‘define default Error page’ link is clicked.
- From the displayed page, default error page is defined.
- The custom error page entry is shown in the config file.
- Finally, the mode attribute is turned on by editing (The tool turned it off by default) the web.config file.
- Using the Google Chrome web browser, the home page is viewed from the solution explorer.
- From the displayed home page the broken web link is clicked.
- In place of showing the system error message (One shown above picture) we see a default error page.
Source Code: Download From Google Drive
- Playing Sound Via C# SoundPlayer & AxWindowsMediaPlayer
- BackGroundWorker Component & Progress Bar of C#
Categories: Asp 2.0
Tags: Custom Error Page, Debugging, Machine.Config, Tracing, web.config
Do you like this Example? Please comment about it for others!!