asp.net web.config Configuration node Detailed _ Basic Application

Source: Internet
Author: User

Web.config file Lookup rules:

(1) If there is a Web.config file in the current page directory, see if there is a node name to look for, if there is a return result and stop looking.
(2) If the Web.config file does not exist in the directory where the current page resides, or if the node name does not exist in the Web.config file, locate its parent directory until the root directory of the Web site.
(3) If the Web.config file does not exist in the root directory of the Web site or if the section name does not exist in the Web.config file%windir% "Microsoft.NET" Framework "v2.0.50727" CONFIG " Find in Web.config file.
(4) If the corresponding node does not exist in the%windir% "Microsoft.NET" Framework v2.0.50727 "CONFIG" Web.config file, then the%windir% "Microsoft.NET" Framework "v2.0.50727" CONFIG "machine.config file.
(5) returns null if it is still not found.
When the ASP.net application is running, the user session information stored in the server's memory is lost (such as sessions stored in memory) if changes to the Web.config file cause the corresponding application to restart.

(i) Appsetings configuration node

<appSettings> nodes are mainly used to store some configuration information of the ASP.net application, such as the way to save the uploaded files.

<appSettings>
 <add key= "imagetype" value= ". jpg; BMP;. GIF;. PNG;. JPEG "/> <!--allow upload of picture format types--> 
</appSettings>
String filetype=configurationmanager.appsettings["FileType"];

(b) <connectionStrings> node

<connectionStrings> node is mainly used to configure the database connection, we can add any node in the <connectionStrings> node to save the database connection string, in the future in the code Dynamically get the value of the node in code to instantiate the database connection object so that once deployed, the database connection information changes we just need to change the configuration here instead of needing to change the program code and redeploy because the database connection information changes

<connectionStrings> 
 <add name= "AspNetStudyConnectionString1" connectionstring= "Data source= (local); Nitial Catalog=aspnetstudy; User Id=sa; Password=sa "/>
</connectionString>
String connectionString = configurationmanager.connectionstrings["AspNetStudyConnectionString1"]. ConnectionString;

(c) <compilation> node

<compilation> 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 it should be set to false after the program has been compiled for use.

(d) <authentication> node

Set asp.net authentication mode, there are four authentication modes, their values are as follows:
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.

(v) <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:
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.

(vi) <error> sub-nodes

The <customErrors> node also contains < error> 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 will take effect , you must set the Mode property of the <customErrors> node node to ' on '. Here is an example:

<customerrors mode= "on" defaultredirect= "genericerrorpage.htm" > <error statuscode= "403" redirect= " 
 403.htm "/>
 <error statuscode=" 404 "redirect=" 404.htm "/>
</customErrors>

(vii)

The

 
 

(ix)

The 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

 
 

(10) <pages> node

The <pages> node is used to represent settings for a specific page, which has three properties, respectively, as follows:
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.

<pages buffer= "true" enableviewstatemac= "true" validaterequest= "false"/>

(11) <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:
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 has the advantage of fast access and a disadvantage of memory consumption, so it is not appropriate to store large user session data in this mode

(12) <globalization> node

Used to configure the globalization settings for an application. This node has several properties that are more important, respectively:
FileEncoding Optional attribute. Sets the storage encoding for. aspx,. asmx, and. asax files.
RequestEncoding Optional attribute. Sets the encoding for the client request, which defaults to UTF-8.
ResponseEncoding Optional attribute. Sets the encoding of the server-side response, which defaults to UTF-8.
The following are the default configurations in the ASP.net application:

<globalization fileencoding= "Utf-8" requestencoding= "Utf-8" responseencoding= "Utf-8"/>

(13) Web.config file reading and writing

public void setappsetting (string key, String value)
{
  appsettingssection appsetting = (appsettingssection) Config. GetSection ("appSettings");
  if (appsetting.settings[key] = = NULL)//If this node does not exist, add 
  {
    appSetting.Settings.Add (key, value);
  else//If this node exists, modify 
  {
    Appsetting.settings[key]. value = value;
  }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.