Listing 161 The Code in the Three testaspxcs Files in the CSharpASPTest Ciass Records and Northwind Folders Used to Test Hierarchical Unmodified webconfig Files

// in CSharpASPTest\test.aspx.cs namespace CSharpASPTest {

// autogenerated code omitted pubiic ciass test : System.Web.UI.Page {

// autogenerated code omitted private void Page Load(object sender, System.EventArgs e) {

Response.Write("In CiassRecords foider<br>"); Server.Transfer("CiassRecords/Northwind/test.aspx");

// in CSharpASPTest\CiassRecords\test.aspx.cs namespace CSharpASPTest.CiassRecords {

// autogenerated code omitted public class test : System.Web.UI.Page {

private void Page Load(object sender, System.EventArgs e) {

Response.Write("In ClassRecords folder<br>"); Server.Transfer("Northwind/test.aspx");

// in CSharpASPTest\Northwind\test.aspx.cs namespace CSharpASPTest.ClassRecords.Northwind {

// autogenerated code omitted public class test : System.Web.UI.Page {

private void Page Load(object sender, System.EventArgs e) {

Response.Write("In Northwind folder<br>"); Response.End();

Compile the project, and then request the test.aspx file in the CSharpASPTest root directory with your browser. Unfortunately, that doesn't work. There are several reasons. The first thing that happens is that an error message appears, alerting you that an error occurred while processing a configuration file (see Figure 16.11).

Server Errr>rh VCShe rpASPTest Applinaton.

pi r Tj nit- ni I "pip. fr rimr [mr WfHf|" .H* f m vw a fflif c^mv^A [■■ A ■"nf^i^umw1 hvn htww fdw be

■ ffwiw-^rtT-w^ni.^«,« ^ Uwl? 1nri nr -ri^^ tptfvii [ 1P1-1 OTZJ

Figure 16.11: Nested configuration file error

The error occurs because you can't set application-level settings within web.config files in subdirectories.

So while web.config files may be hierarchical, there are some stated but well-hidden rules. The error occurs in the first-level subdirectory of the CSharpASPTest root—the ClassRecords directory. Remove the application-level settings. Here are the sections you must remove from web.config files in your application's subdirectories:

After removing the sections, run the application again. This time, you get a different error when trying to use the Server.Transfer method to switch execution to the Northwind/test.aspx file. The server can't find the file. The reason is that while the executing file is in the classRecords folder, the context under which the application is running is still that of the original request—the test.aspx file in the root folder. If you're going to use the ServerTransfer, it's important to understand that it does not work quite the same way as the Response.Redirect method. Response.Redirect forces the browser to request the file; therefore, the context for the next file is appropriate to that file. In contrast, the browser is completely unaware of any Server.Transfer command, because when the Transfer method occurs, the browser still hasn't received a response.

The other settings in the web.config file may not have any effect, but they don't cause errors.

Add this key to the <appSettings> section of the web.config file in the root directory. Create the <appSettings> section if it doesn't already exist.

<add key="test" value="root"/>

Similarly, add "test" keys to the <appSettings> section for the web.config files in the

ClassRecords and Northwind folders.

<add key="test" value="ClassRecords"/> <add key="test" value="Northwind"/>

Create three new Web Forms, one in each subdirectory of the application, and call them test2.aspx. These files are identical to the test.aspx Web Forms except that each has an additional line to retrieve and display the value of the "test" key in its web.config file (see Listing 16.2).

Was this article helpful?

0 0

Post a comment