Web. configAn XML text file is used to store ASP. NET Web application configuration information, such as the most common settings ASP. NET Web application authentication method), which can appear in every directory of the application. When you use VB. after creating a Web application, a default Web application is automatically created in the root directory by default. config file, including the default configuration settings. All subdirectories inherit its configuration settings. If you want to modify the configuration settings of a subdirectory, you can create a Web. config file under the subdirectory. It can provide configuration information other than the configuration information inherited from the parent directory, or rewrite or modify the settings defined in the parent directory.
Modifications to the Web. config file at runtime can take effect without restarting the service. Note: <processModel> exception section ). Of course, the Web. config file can be expanded. You can customize new configuration parameters and write the configuration section handler to process them.
1. default configuration settings of the web. config configuration file)
All the following code should be located in
<Configuration>
<System. web>
And
</System. web>
</Configuration>
For the purpose of learning, the following examples omit this XML tag.
1. <authentication> section
Purpose: Configure ASP. NET authentication to support Windows, Forms, PassPort, and None ). This element can only be declared at the computer, site, or application level. The <authentication> element must be used with the <authorization> section.
Example:
In the following example, the website is configured for authentication based on form Forms. When a user who does not log on to the website that requires authentication, the webpage automatically jumps to the login webpage.
- <authentication mode="Forms" >
- <forms loginUrl="logon.aspx" name=".FormsAuthCookie"/>
- </authentication>
The element loginUrl indicates the name of the login webpage, and name indicates the Cookie name.
2. <authorization> section
Purpose: control access to URL resources from clients. For example, Anonymous Users are allowed to access URL resources ). This element can be declared on any level of computer, site, application, subdirectory or page. Must be used with the <authentication> section.
Example: The following example disables access by anonymous users:
- <authorization>
- <deny users="?"/>
- </authorization>
Note: You can use user. identity. to obtain the authenticated user name. You can use the web. security. formsAuthentication. the RedirectFromLoginPage method redirects authenticated users to the page the user just requested.
3. <compilation> section
Purpose: configure all compilation settings used by ASP. NET. The default debug attribute is "True". After the program is compiled and delivered, it should be set to the TrueWeb. config file for detailed instructions. The example is omitted here)
4. <customErrors>
Purpose: Provide information about custom Errors for ASP. NET applications. It is not applicable to errors in XML Web services.
Example: When an error occurs, redirect the webpage to the custom error page.
- <customErrors defaultRedirect="ErrorPage.aspx" mode="RemoteOnly">
- </customErrors>
The defaultRedirect element indicates the name of the custom error webpage. Mode element: displays custom (friendly) information for users not running on the Local Web server.
5.
Purpose: Configure ASP. net http Runtime Library settings. This section can be declared at the computer, site, application, and subdirectory level.
For example, the maximum size of a file to be uploaded is 4 MB, the maximum time is 60 seconds, and the maximum number of requests is 100.
-
6. <pages>
Purpose: Identify page-specific configuration settings, such as whether to enable session Status, view status, and whether to detect user input ). <Pages> statements can be made at the computer, site, application, and subdirectory level.
Example: do not detect potential dangerous data in the content entered by the browser. Note: This item is used by default, 1. encode or verify user input). When you send a page from the client, the encrypted view status is checked to verify that the view status has been tampered with on the client. (Note: This item is not verified by default)
- <pages buffer="true" enableViewStateMac="true" validateRequest="false"/>
7. <sessionState>
Purpose: configure the session Status settings for the current application, such as whether to enable the session Status and the location where the session status is saved ).
Example:
- <sessionState mode="InProc" cookieless="true" timeout="20"/>
- </sessionState>
Note:
Mode = "InProc" indicates that the session status can be stored locally on a remote server or SAL server, or the session status is not enabled)
Cookieless = "true" indicates that session status is enabled if the user's browser does not support cookies (the default value is False)
Timeout = "20" indicates the number of minutes in which the session can be idle.
8. <trace>
Purpose: configure the ASP. NET tracking service, which is mainly used for program testing to identify errors.
Example: The default configuration in Web. config is as follows:
- <trace enabled="false" requestLimit="10" pageOutput="false" traceMode="SortByTime" localOnly="true" />
Note:
Enabled = "false" indicates that tracing is not enabled; requestLimit = "10" indicates the number of Trace Requests stored on the server.
PageOutput = "false" indicates that the trace output can only be accessed through the tracking utility;
TraceMode = "SortByTime" indicates that trace information is displayed in the order of processing traces.
LocalOnly = "true" indicates that the trace Viewer (trace. axd) is used only for the host Web server.
Ii. custom Web. config file configuration section
The configuration section of the custom Web. config file consists of two steps.
First, declare the name of the configuration section and the name of the. NET Framework class that processes the configuration data in the section between the <configSections> and </configSections> labels at the top of the configuration file.
Second, make actual configuration settings for the declared section after the <configSections> area.
Example: Create a storage 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="scon" value="server=a;database=northwind;uid=sa;pwd=123"/>
- </appSettings>
- <system.web>
- ......
- </system.web>
- </configuration>
3. access the Web. config file
You can use the ConfigurationSettings. deleettings static string set to access the Web. config file example: Obtain the connection string created in the preceding example.
4. Create a Web. config file
1. In Solution Explorer, click the refresh icon to confirm that the application does not have the Web. config file.
If you have used the website management tool or some other methods to configure the application, the Web. config file may have been created automatically. Click "refresh" to update the file list.
2. In Solution Explorer, right-click the website name and click Add new project ".
3. In the "template" window, click "Web configuration file ".
The file name in the "name" text box should be Web. config. You can provide other names for this file, but this is the default name .. The config File Extension prevents ASP. NET from downloading files.
4. Click "add" to create the file and edit it.
This file contains the code displayed in the "example" section after this topic and has some initial default values. Application from % SystemRoot % \ Microsoft. NET \ Framework \ <version> \ CONFIG directory Machine. config and Web. the config File inherits all configuration settings, but these default settings are not displayed here. If you want to override the inherited default settings or add a collection of elements such as the httpHandlers element ASP. NET setting architecture), you only need to create a Web. config file at the application level and directory level.
To view all the configuration settings of the current application, you can run the topic: programmatically view the Code contained in the inherited configuration settings and local configuration settings. You can also view % SystemRoot % \ Microsoft. NET \ Framework \ <version> \ CONFIG directory Machine. config. comments or Web. config. the comments file also contains useful comments), but these two files won't contain all runtime settings. See how: programmatically view the inherited configuration settings and local configuration settings.
5. If the Web. config file is changed, save the file.
Saving the Web. config file restarts the application. You can also use the configSource attribute of a single section element to point to a secondary configuration file. Changing the secondary configuration file will not cause the application to restart. For more information, see configSource in the general attributes inherited by the section element.
Web. config is an important configuration file in the asp.net application. You can use the Web. config file to conveniently develop and deploy the asp.net application. I hope this article will help you with your reference.