Add Custom Settings

You can customize the web.config file with new sections. This makes the web.config file flexible enough to contain all of your configuration information in a structured way. The custom configuration section information is placed into two main areas in the configuration file.

The first area is for declaring names, groups, and handles for the custom sections. Place configuration section declarations in the <configSections> container tags. You will have select groups,

<sectionGroup>, and section <section> tags for defining the structure of your custom configuration information. The <sectionGroup> is used to give hierarchy to your <section> tags. The <section>

tags in your declaration have two properties, type and name. The type attribute is the name of the class that reads the information, for example, System.Configuration.NameValue SectionHandler class is a structure for name value pairs. The name attribute is the name of the tag that contains the information the section handler will read.

Now that the sections and section groups are defined, you can store the custom configuration information by placing the <sectionGroup> and <section> tags under the <configuration> root node. With these tags, you will add the custom information in the <section> tag attributes.

ADD CUSTOM SETTINGS

ADD CUSTOM SETTINGS

LH Add the <configSections> tags.

< Add the <sectionGroup> Add the <goalsetup> tag tag and set the name attribute within the <system.web> tags. to system.web.

U Add the <add> tag and set the key attribute to maxnumber and the value attribute to 10.

LH Add the <configSections> tags.

Note: If you are creating a new web.config file, first add the <configuration> tags.

< Add the <sectionGroup> Add the <goalsetup> tag tag and set the name attribute within the <system.web> tags. to system.web.

_Q Add the <section> tag and set the type attribute to System.Configuration. NameValueSectionHandler, System.

U Add the <add> tag and set the key attribute to maxnumber and the value attribute to 10.

CONFIGURE YOUR ASP.NET APPLICATIONS

Extra

You may have noticed a couple of peculiar things about the source code for retrieving the value in the web.config file. Notice the use of (String) and (NameValueCollection). These are classes defined for reading the configuration information. For example, the value retrieved from

Context.GetConfig("system.web/goalss etup") is cast into a NameValueCollection type variable and then the value for the "maxnumber" is cast into a string.

You can access custom configuration information you use the HTTPContext class in the

System.Web namespace

(Context.GetConfig("system.web/goals setup"). The GetConfig gives you access to the web.config file. You need to specify the path to the node that you wish to work with, in this case, "system.web/goalssetup".

You can put this configuration information into a Session variable. This would make sense if you were modifying the value of the configuration information. For example, the maxnumber could be read into a Session variable when the user logs into the Web site. Based on the actions of the user, you could update the value of the Session variable.

| Add a message to the user.

— Save the file and request it from the Web server.

■ A message appears that displays the maxnumber custom setting from the web.config file.

Q Open the

GenericTemplate.aspx template from the Code Templates directory.

LQ Add a heading for the page.

| Add a message to the user.

Write the value of the maxnumber configuration.

— Save the file and request it from the Web server.

■ A message appears that displays the maxnumber custom setting from the web.config file.

ENABLE PAGE-LEVEL DEBUGGING

If you are having problems with a page during development, you can turn page-level debugging on for the page to get more detailed error information, including the line number and the source code associated with the error. ASP.NET can configure debugging at the page level.

On the page you want to debug, add a <@ Page debug="true" > directive to the top of the page. The next time the page is requested and an error occurs on the page, detailed error information is displayed. Without this directive, you will get an error page that does not have any relative error information that can assist you in understanding why the error occurred.

Only pages that have the debug attribute set on the @Page directive are compiled into debug mode. If you want all pages in the site to run in debug mode, you need to update the web.config file. To enable page debugging application-wide, you need to add the compilation element under the <system.web> tag and set the debug attribute equal to true.

Make sure that you only turn on page debugging when necessary. Running applications in debug mode does incur a memory/performance overhead. For most cases you should not enable this in production.

ENABLE PAGE-LEVEL DEBUGGING

ENABLE PAGE-LEVEL DEBUGGING

Q Open the

DatagridTemplate.aspx template from the Code Templates directory.

B Create an error on the page by changing the SQL statement to an invalid SQL statement.

-■ The page displays and an error message appears.

■ A message appears from the server with a description of the error; however, no details are given as to on which line the error occurred.

Q Open the

DatagridTemplate.aspx template from the Code Templates directory.

B Create an error on the page by changing the SQL statement to an invalid SQL statement.

0 Save the page and request it from the Web server.

-■ The page displays and an error message appears.

■ A message appears from the server with a description of the error; however, no details are given as to on which line the error occurred.

DEBUG YOUR ASP.NET APPLICATIONS

When page-level debugging is set, you may receive several pieces of information to help you play detective when an error occurs. Here are the elements of the error message:

ELEMENT

DESCRIPTION

Description

A brief description of the error.

Exception Details

Indicates which exception was raised and describes exception.

Source Error

Displays a couple of lines of the source code before and after the line that generated the error.

Source File

Specifies the path and filename of the source file that generated the error.

Line

Indicates the line number of the error.

Stack Trace

Displays the call stack for the error.

Version Information

Displays the Runtime Build and the ASP.NET Build.

_Q Return to the page you 0 Save the page and request ■ The page displays and an were creating and add the it from the Web server. error message appears.

@Page Debug trace directive.

■ A message appears from the server with a description of the error and the line number and the source code where the error occurred.

_Q Return to the page you 0 Save the page and request ■ The page displays and an were creating and add the it from the Web server. error message appears.

@Page Debug trace directive.

■ A message appears from the server with a description of the error and the line number and the source code where the error occurred.

Was this article helpful?

0 0

Post a comment