. NET provides a configuration for the current machine. ---name: machine.config
Running mechanism: ASP.net Web site When IIS starts, it loads the configuration information from the configuration file and caches the information so that you do not have to read the configuration information each time. The asp.net application monitors the configuration file changes during the run, and once you edit the configuration information, the configuration information is re-read and cached.
Configuration file node:
Must understand: The Web.config file is an XML file
Name of the root node to which it belongs:
Copy Code code as follows:
<configuration>
Child node:<configsections></configsections>
<appSettings> </appSettings>
<connectionStrings></connectionStrings>
<system.web></system.web>
Special: <system.webServer></system.webServer>-----is used to specify the IIS 7.0 settings for WEB applications, only for IIS 7 set-mode is not applicable to Classic mode. If the application is running in Classic mode, the Web.config file is ignored
<configuration>
Node Analysis:
1.<connectionstrings> Node
Mainly used to configure the database connection, you can add any node in the <connectionStrings> node to save the database connection string and then dynamically get the value of the node through the code to instantiate the database connection object.
For example:
Copy Code code as follows:
<connectionStrings>
<!--SQL Server database configuration--> <add name= "You want to take the name (arbitrary)" connectionstring= "Date source= database login; Initial catalog= database name; User id= logon person password= password "/> is a connection that can configure multiple databases
<add name= "AspNetStudyConnectionString1" connectionstring= "Data source= (local); Initial Catalog=aspnetstudy; User Id=sa; Password=sa "/>
<add name= "ConnectionString" connectionstring= "Data source=10.0.0.52;initial Catalog=new_hospital_dev; User id=hospital;pwd=hospital; " />
</connectionStrings>
In Code Chinese Medicine reads database connection objects Practices:
Copy Code code as follows:
Read Web.config node configuration
String connectionString = configurationmanager.connectionstrings["AspNetStudyConnectionString1"]. ConnectionString;
Instantiating a SqlConnection object
SqlConnection connection = new SqlConnection (connectionString);
You can see the benefits: Once the database used for development is inconsistent with the database at deployment time, just edit the value of the ConnectionString property with a text editing tool such as Notepad.
2.<appsettings> node
Function: Store Some configuration information of asp.net application, such as the save path of uploading file
For example, you can configure a picture type:
Copy Code code as follows:
<appSettings>
<add key= "imagetype" value= ". jpg; BMP;. GIF;. PNG;. JPEG "/>----<!--allow uploading of picture format types-->
<add key= "FileType" value= ". jpg; BMP;. GIF;. PNG;. JPEG;. PDF;. Zip;. rar;. XLS;. Doc "/><!--allowed file types to be uploaded-->
</appSettings>
Read the values in the </appSettings> node:
String filetype= configurationmanage.appsettings["FileType"]; ----is actually a value for the form of a key-value pair.
3.<system.web> node:
Subordinate child node:
3.1.<compilation> node
Function: The node configures all compilation settings used by asp.net. The default Debug property is "True", which allows debugging, which in this case affects the performance of the Web site so that it should be set to false after the program has been compiled and delivered to use
For example:
Copy Code code as follows:
<compilation targetframework= "4.0" debug= "true" >
<buildProviders>
<add extension= ". html" type= "System.Web.Compilation.PageBuildProvider"/>
</buildProviders>
</compilation>
3.2.<authentication> node
Role: Controls user access to a Web site, directory, or individual page
Set asp.net authentication mode, there are four authentication modes, their values are as follows:
Mode description
Windows uses Windows authentication to apply to domain users or local area network users.
Forms uses form validation and relies on Web site developers for authentication.
Passport authenticates by using the authentication service provided by Microsoft.
None does not perform any authentication.
For example: <!--request Mode-->
<authentication mode= "Forms"/>
3.2.<customerrors> node
The <customErrors> node is used to define information about some custom error messages. This node has the mode and defaultredirect two properties, where the Defaultredirect property is an optional property that represents the default URL that is redirected to when an application error occurs, and a generic error is displayed if the property is not specified. The Mode property is a required attribute that has three possible values, and they represent the following meanings:
Mode description
On indicates that custom error messages are visible to both local and remote users.
Off disables custom error messages, and local and remote users will see detailed error messages.
RemoteOnly indicates that the local user will see the detailed error message, and the remote user will see the custom error message.
There is a need to explain the concepts of local and remote users. The machine used when we access the ASP.net application becomes the local user when the machine used for publishing the ASP.net application is the same machine, whereas the other is called a remote user. Setting the Mode property to On or RemoteOnly during the deployment phase in order to easily find the error mode property is recommended in the development debugging phase, to avoid these detailed error messages exposing the details of the program code and causing the hacker's intrusion.
Example:
Copy Code code as follows:
<!--error jump mode= "RemoteOnly"-->
<customerrors mode= "Off" defaultredirect= "/error.aspx" >
<error statuscode= "404" redirect= "/404.aspx"/>
<error statuscode= "redirect="/500.aspx "/>"
</customErrors>
3.3<error> child nodes
The <customErrors> node also contains <error> child nodes, which are redirected to our custom error page based on the HTTP error status code of the server, and note that the configuration under the <error> sub node is in effect, you must set the Mode property of the <customErrors> node node to ' on '. Here is an example:
Copy Code code as follows:
<customerrors mode= "on" defaultredirect= "genericerrorpage.htm" >
<error statuscode= "403" redirect= "403.htm"/>--if the user does not have permission to access the requested page, it jumps to the 403.htm page
<error statuscode= "404" redirect= "404.htm"/>--if a user accesses a page that does not exist, it jumps to the 404.htm page
</customErrors>
403.htm and 404.htm pages are the pages we add ourselves
3.4
Function: To give the user's request to the appropriate handler based on the URL and HTTP predicate requested by the user, and the result is that the user cannot view or download the relevant file
If a file in one of our folders or a type of file does not allow the user to download it, you can add the corresponding child nodes to the
Example: We create a ipdata directory in our asp.net application, creating a IPData.txt file in the Ipdata directory, adding the following configuration to the Web.config
Copy Code code as follows:
<add path= "Ipdata/*.txt" verb= "*" type= "System.Web.HttpForbiddenHandler"/>---Code is prohibited from accessing any TXT file in the Ipdata directory
<add path= "*.mdf" verb= "*" type= "System.Web.HttpForbiddenHandler" validate= "true"/>
<add path= "*.ldf" verb= "*" type= "System.Web.HttpForbiddenHandler" validate= "true"/>---for *.mdf, *.ldf files, Get or POST requests will be handed to System.Web.HttpForbiddenHandler to process, the user can not view or download the relevant files
3.5.
Used to set the ASP.net HTTP run-time library. This section can be declared at the computer, site, application, and subdirectory levels.
For example, the following configuration controls the maximum number of files the user can upload is 40M (40*1024k), the maximum timeout is 60 seconds, and the maximum concurrent request is 100.
3.6.<pages> node
Used to indicate setting for a specific page, with three properties
Attribute Name Description
Buffer whether HTTP response buffering is enabled.
enableViewStateMac Whether the computer authentication check (MAC) should be run on the page's view state to place user tampering, default to False, and performance degradation if set to true.
ValidateRequest whether to verify that there are Cross-site scripting attacks and SQL injection exploits in user input, the default is true, and if a match occurs, a HttpRequestValidationException exception is sent. Set this property to False for the inclusion of an online text editor page that typically authenticates user input.
Here is an example of a configuration node:
<pages buffer= "true" enableviewstatemac= "true" validaterequest= "false"/>
3.7<sessionstate> node
The <sessionState> node is used to configure the session-state configuration for the current asp.net application. Here's a common configuration:
<sessionstate cookieless= "false" mode= "InProc" timeout= "/>"
The node configuration above is set to enable cookies in the ASP.net application, and the session state mode is specified to save session state in the process, and a session timeout of 30 minutes is also specified.
The mode attribute of the <sessionState> node can be one of several values:
Attribute Value Description
Custom data was used by custom to store session-state data.
InProc default value. The session-state data is stored by the ASP.net worker process.
Off disables session state.
SQL Server saves session state data using an Out-of-process SQL Servers database.
StateServer uses the out-of-process asp.net State service to store state information.
Typically, by default, the InProc mode is used to store session-state data, which benefits from fast access and the disadvantage of memory consumption, so it is not appropriate to store large user session data in this mode.
4<system.webserver>
----------------------
<defaultDocument>
Role: IIS 7.0 provides a default file when the request URL does not contain a specific file for the Web application.
Within the system.webserver element, create a defaultdocument element.
Within the defaultdocument element, create a files element.
Creates an add element within the files element and specifies the path and name of the default file within the Value property.
Copy Code code as follows:
<configuration>
<system.webServer>
<defaultDocument> <files> <add value= "products.aspx"/> </files> </defaultd Ocument>-----Configure the default file as the default file for providing products.aspx files
</system.webServer>
</configuration>