Listing 162 The Code in the Three testaspxcs Files in the CSharpASPTest Class Records and Northwind Folders Used to Read Application Settings from Hierarchical webconfig Files

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

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

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

Response.Write("In CSharpASPTest folder<br>"); Response.Write("Application setting \"test\"=" + System.Configuration.ConfigurationSettings. AppSettings.Get("test") + "<br>"); Server.Transfer("ClassRecords/test2.aspx");

// in CSharpASPTest\ClassRecords\test2.aspx.cs namespace CSharpASPTest.ClassRecords {

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

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

Response.Write("In ClassRecords folder<br>"); Response.Write("Application setting \"test\"=" + System.Configuration.ConfigurationSettings. AppSettings.Get("test") + "<br>"); Server.Transfer("ClassRecords/Northwind/test2.aspx");

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

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

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

Response.Write("In Northwind folder<br>"); Response.Write("Application setting \"test\"=" + System.Configuration.ConfigurationSettings. AppSettings.Get("test") + "<br>"); Response.End();

Compile the project again and then request the test2.aspx file in the CSharpASPTest root directory with your browser. This time, you'll see the output in Figure 16.12.

VVM'.VH

Figure 16.12: Test hierarchical web.config files—display application settings

VVM'.VH

Figure 16.12: Test hierarchical web.config files—display application settings

Not exactly what you might expect, but again, the point of this exercise is that you must take into account the context in which a page executes. Despite the fact that the code transfers execution to the ClassRecords/test.aspx Web Form and then to the ClassRecords/Northwind/test.aspx Web Form, the context for application-level settings is the web.config file in the root CSharpASPTest folder—that's why you see the output Application setting test="root" regardless of which file executes.

At this point, you might think that the web.config files in the application subdirectories aren't really valid. If you read the documentation carefully, it seems that web.config files apply only to virtual directories. However, that's not entirely true. Change the test2.aspx.cs file in the ClassRecords directory so the Page_Load event reads this way:

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

Response.Write("In ClassRecords folder<br>"); Response.Write("Application setting \"test\"=" + System.Configuration.ConfigurationSettings. AppSettings.Get("test") + "<br>"); Server.Transfer("Northwind/test2.aspx");

Note the changed Server.Transfer line. Recompile the application. Now, request the CSharpASP-Test/ClassRecords/test2.aspx file directly from a browser. Hmm. Not only does the file run without errors, but it does read the web.config file settings in the ClassRecords folder, even though the directory is not marked as a virtual directory (you'll see more about that in the next section). That seems unusual because, although config files are supposed to be hierarchical, you might expect that a section named <appSettings> would be limited to the application's root directory, but that's not what happens. The output shows that the context, as expected, is the ClassRecords directory this time, even after the Server.Transfer takes effect and the test2.aspx file in the Northwind folder executes.

The point here is that some web.config file settings—even in plain (non-virtual) subdirectories do override settings at higher levels, exactly as the documentation (sort of) states.

Note One quick aside: If you change the Server.Transfer line in the

ClassRecords.test2.aspx.cs file to Server.Transfer("../test2.aspx") , transferring execution "up" to the application's root directory, you get a recursion error. In other words, you cannot transfer to another file and then transfer back. In fact, you can't use Server.Execute(../test2.aspx) to do that either, because the root test2.aspx.cs file transfers execution back—and you still get the recursion error. If you comment out the Server.Transfer line in the root test2.aspx.cs file, the code works fine, and the context remains that of the ClassRecords/test2.aspx file, so there's no problem with transferring execution from a lower to a higher point in the application folder hierarchy.

Although I haven't yet tested every standard section and setting in the web.config file, the only ones that seem to have any effect at subdirectory levels are those that deal with application settings, security, and data stored in custom sections. You'll see more about security in Chapter 18, "Controlling Access and Monitoring," and I'll show you how to create custom settings in the "Creating Custom Configuration Sections" section later in this chapter.

Was this article helpful?

0 0

Post a comment