Broken Link On Home Page

Web Config File and Tracing in ASP.Net

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:

ASP.Net Default Config Files
ASP.Net Default 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:

Web.Config File Distribution
Web.Config File Distribution

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:

  1. http://www.C-sharpCorner.com
  2. http://www.C-sharpCorner.com\DBCorner
  3. http://www.C-sharpCorner.com\DBCorner\VBCorner

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:

  1. Machine.config file from the windows\DotNetFramework Folder (See Picture above)
  2. Web.Config from the same folder from the above folder
  3. The Web.Config from the D:\C-AskCorner.com folder
  4. Web.config from the DbCorner folder
  5. 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:

About The Example
About The Example

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.

Error Thrown by IIS Server
Error Thrown by IIS Server

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.


Video Steps

  1. First, we add a Web Form for Error page to the Project.
  2. Then, we add a Label to this Web Form.
  3. 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.


Video Steps

  1. The video selects already placed hyperlink control and invokes properties for it.
  2. 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:

ASP.Net Debugging Not Enabled Window
ASP.Net Debugging Not Enabled Window

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:

Debug Enabled Web.Config File
Debug Enabled 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:


Video Steps

  1. First we click the Start Debugging from the debug menu.
  2. Next we click OK to the dialog to Turn-On the debugging.
  3. 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.

Turn On Tracing for Asp.net Application
Turn On Tracing for Asp.net Application

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:

Tracing Entry in Web.Config File
Tracing Entry in Web.Config File

The videos given below shows how to turn ON the trace feature and Capture Trace information:


Video Steps

  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 is clicked.
  3. From the displayed page, we set the tracing requirements.

Video Steps

  1. First we open the home page (The Page with all 4 links).
  2. Then from the context menu, we browse the page through Internet Explorer.
  3. 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:

Broken Link On Home Page
Broken Link On Home Page

 

System Supplied Error Message
System Supplied Error Message

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.


Video Steps

  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.

Video Steps

  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.

Source Code: Download From Google Drive

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.