Configuring an Application

One element seems to be missing from the Visual Basic .NET code shown in Listing 4-3. Although I've often extolled the virtue of using Option Explicit, I didn't use it in this example, which can be done by setting Explicit="true" in @ Page. I can assure you that the page does require variables to be declared—I missed one of the references to loop when converting the application from C# to Visual Basic .NET and an error page did in fact appear, as shown in Figure 4-13.

11 rrrm

» t> «


- —

J J 2 T-----*» Jmtfi J-H^n. } • J i

tt k>-

H .i h "

Server Error in V Application,

Campiiation Frrar

Server Error in V Application,

Campiiation Frrar

imitarlmr Htait-MH-i IK "a"" LnrKfc4viir<mrim UiAl IffH]

Lirw :i: ir»T 512t"[!i]J:-»H I " J ii»-l l4.'T»iInii i-"

L ifm 33! tlWRAlii toim« i'rfr:-: i* .iie ri-Ti-r-r- Lhk:11

Figure 4-13 : The error message that appears when a variable is not declared in the source from Listing 4-3

This error message contains quite a bit more information than ASP error messages had, and it includes the section of code that produced the error, with the line in which the error occurred displayed in red. By default, this detailed error message will appear only on the machine on which the application is running. This is good default behavior because it's possible that the source code displayed could include information about database user names and passwords, or worse.

So if I didn't set Explicit to true, why did I get the error message about the undeclared variable (badly reported in this example as "Expected an expression")? The answer is the Web.config file. In addition to specifying Explicit and Strict on each page, Web.config provides an application-global way to configure these and other application settings. Listing 4-5 shows a simple Web.config file. Listing 4-5 Simple Web.config file, with explicit and strict set to "true", eliminating the need for setting these on each Visual Basic .NET page

<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.web> <compilation debug="true" defaultLanguage="C#" explicit="true" batch="true" batchTimeout="30" strict="true" > </compilation> </system.web>


If you're familiar with XML, you'll recognize Listing 4-5 as a simple, well-formed XML document. In Chapter 8, we'll discuss XML, but in the meantime, you'll need to be aware of the following aspects of XML:

■ XML always uses matched start and end tags, unlike HTML, which allows omission of many end tags. For some tags, the start and end tags can be represented as a single tag. For example, the following tag is valid:

<compilation debug="true" />

■ XML is case-sensitive. Thus, the following tag is not valid, because </Compilation> is not seen as a closing tag for <compilation>:

<compilation debug="true"> </Compilation> Although early betas of the .NET Framework were forgiving about the case of attributes values (for example, true and True were considered the same), from Beta 2 onward, the configuration files are, correctly, completely case-sensitive.

■ XML values are enclosed in quotation marks. Thus, the following tag is not valid:

<compilation debug=true />

XML is the preferred data language of the .NET Framework. As of this writing, however, no automated administrative tool is available for editing the Web.config file. This isn't a terrible problem because XML is an easy format to follow, but it does mean that you'll need to manually tweak these files using a normal editor program such as Notepad (known affectionately as Notepad.NET to some of the early ASP.NET adopters). I won't cover all the many possible configuration options because they're described in the .NET Framework Software Development Kit (SDK) documentation; however, I will go through the important configuration options and their implications and explain each of the sections of the Web.config file.

Was this article helpful?

0 0

Post a comment