A web Web.config file is an XML file that stores configuration information for ASP.net Web applications (such as the most common settings asp.net the way the Web application is authenticated) and can appear in every directory in the application. When you create a new Web application through Visual Studio.NET, a default Web.config file is automatically created in the root directory by default, including the default configuration settings, and all subdirectories inherit its configuration settings. If you want to modify the configuration settings for subdirectories, you can create a new Web.config file in the subdirectory. It can provide configuration information in addition to the configuration information inherited from the parent directory, or you can override or modify the settings defined in the parent directory.
When a Web application is running, modifications to the Web.config file do not require a restart of the service to take effect (note the exception to the:<processmodel> section). Of course, Web.config files can be extended. You can customize the new configuration parameters and write configuration section handlers to process them.
web.config configuration file
Web.config is an XML file, and all configuration information is recorded with the corresponding node. All the following code in the configuration file should be in the root node below:
<configuration>
<system.web>
......
......
</system.web>
</configuration>
Easy to learn and save space, the book omits other node code, readers can find it from the directory of each Web application, with a text editor can be opened to see. Here are a few important nodes to introduce.
(1) <authentication> node
Role: Configure ASP.net authentication support (for Windows, Forms, PassPort, none four). This element can only be declared at the computer, site, or application level. <authentication> elements must be used in conjunction with the <authorization> section.
(2) <authorization> node
Role: Controls client access to URL resources (such as allowing anonymous users to access). This element can be declared at any level (computer, site, application, subdirectory, or page). Must be used in conjunction with the <authentication> section.
(3) <compilation> Festival
Role: Configure all compilation settings used by asp.net. The default Debug property is "true". You should set it to True after the program compilation has been delivered and used (in the Web.config file for detailed instructions, omit the example)
(4) <customErrors> node
Role: Provides information about custom error messages for ASP.net applications. It does not apply to errors that occur in XML Web services.
For example, when an error occurs, the page is jumped to a custom error page.
<customerrors defaultredirect= "errorpage.aspx" mode= "RemoteOnly" >
</customErrors>
where element defaultredirect represents the name of the custom error page. The mode element indicates that custom (friendly) information is displayed for users who are not running on the local Web server.
(5)
Role: Configure ASP.net HTTP runtime settings. This section can be declared at the computer, site, application, and subdirectory levels.
For example: Control user upload file maximum is 4M, the longest time is 60 seconds, the maximum number of requests is 100.
(6) <pages> node
Role: Identifies page-specific configuration settings (such as whether session state is enabled, view state, whether user input is detected, and so on). <pages> can be declared at the computer, site, application, and subdirectory levels.
For example, do not detect potential risk data for user input in the browser (note: The default is detection, if you use no detection, to encode or verify the user's input, the encrypted view state is checked when the page is sent back from the client to verify that the view state has been tampered with on the client. (Note: This key is not validated by default).
<pages buffer= "true" enableviewstatemac= "true" validaterequest= "false"/>
(7) <sessionState> node
Role: Configures session state settings for the current application (such as setting whether session state is enabled, and where the session state is saved).
For example:
<sessionstate mode= "InProc" cookieless= "true" timeout= "/>"
</sessionState>
What needs to be stated is:
Mode= "InProc" means: Store session state locally (you can also choose to store on a remote server or a SAL server or not enable session state);
Cookieless= "true" means that session state is enabled when a user's browser does not support cookies (default is false);
timeout= "20" means the number of minutes that a session can be idle;
(8) <trace> node
Role: Configure the ASP.net tracking service, mainly used to determine where the program test error.
For example: The following is the default configuration in Web.config:
<trace enabled= "false" requestlimit= "ten" pageoutput= "false" tracemode= "SortByTime" localonly= "true"/>
What needs to be stated is:
Enabled= "False" indicates that tracing is not enabled; requestlimit= "10" indicates the number of trace requests stored on the server;
Pageoutput= "false" means that trace output can only be accessed through the trace utility;
Tracemode= "SortByTime" indicates that trace information is displayed in the order in which the trace is processed;
Localonly= "True" indicates that the trace viewer (Trace.axd) is used only for the hosting Web server.
Customizing the Web.config file configuration section
The custom Web.config file configuration section process is divided into two steps:
(a) Declare the name of the configuration section and the name of the. NET Framework class that handles the configuration data in the section between the top <configSections> and </configSections> tags of the configuration file.
(b) Make the actual configuration settings for the declared section after the <configSections> area.
For example: Create a section to store a database connection string
<configuration>
<configSections>
<section name= "appSettings" type= "System.Configuration.NameValueFileSectionHandler, System, version=1.0.3300.0, Culture=neutral, publickeytoken=b77a5c561934e089 "/>
</configSections>
<appSettings>
<add key= "Sqlcon" value= "Server=aspnet;database=northwind;uid=sa;pwd=1234567abc"/>
</appSettings>
<system.web>
......
</system.web>
</configuration>
accessing node information for web.config files
You can access the Web.config file sample by using a collection of configurationsettings.appsettings static strings: Get the connection string established in the previous example.
Dim Sqlcon as String = ConfigurationSettings.AppSettings ("Sqlcon")
Dim Sqlcon = New SqlConnection (Sqlcon)