Configuration files in ASP. NET and ASP. NET
Historical Review:
The abstract factory and reflection were used to implement database replacement during the IDC room charging system. In the factory, first define the Assembly name and namespace name, and write the Assembly name and namespace value in the configuration file, when the execution of the Factory function is performed, the program automatically reads the corresponding characters in the configuration file and generates the corresponding objects according to the path instance.
<appSettings>
<add key="ConnStr" value="Data Source=192.168.24.94;Initial Catalog=MyCharge_sys;User ID=sa;Pwd=123456" ></add> <add key="DBString" value="Sql" ></add></appSettings>
In this way, we don't have to write a connection statement every time we connect to the database, and we can use reflection to directly modify the configuration file after the database is changed. The configuration file can be opened and modified outside the system, you can use any text editing tool.
In the niugu news publishing system, when a system error is set, the page automatically jumps to the error page. All error pages in the system automatically jump to this error page.
<System. web> <! -- Automatic orientation when an error occurs --> <customErrors mode = "On" defaultRedirect = "~ /Error.html "> </customErrors> </system. web>
My understanding of the configuration file:
The configuration file is created when the user logs on to the computer for the first time and stored on the local hard drive of the computer. Set of settings and files when users set and use software. It includes user-specific configuration settings, such as program items, screen colors, network connections, printer connections, mouse settings, and window size and position.
ASP. NET goodbye configuration file
There are two main types:
1. Machine. Config
Each computer can have only one Machine. Config file applied to all applications residing on the server. Because ASP. NET Runtime is to load a lot of configuration files, of course, Machine. config must be loaded, but some of the configurations are not required, so to shorten ASP. NET loading events can be moved to other places, and loaded if used.
2. Web. Config
Each application can have a Web. Config file that overwrites the settings of Machine. Config. Application to a single application residing on the server
Detailed explanation of Web. Config
Web. config is an XML text file used to store ASP. NET Web application configuration information (for example, the most common settings ASP. NET Web application authentication method), which can appear in every directory of the application. When in 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. 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. The Web. config file can be expanded. You can customize new configuration parameters and write a configuration section handler to process them.
Several elements:
(1) <authentication>
Purpose: Configure ASP. NET authentication support (Windows, Forms, PassPort, None ). This element can only be declared at the computer, site, or application level. The <authentication> element must be used with the <authorization> section.
For example, if a user does not log on to the web page that requires authentication, the web page automatically jumps to the login web page.
<authentication mode="Forms" > <formsloginUrl="logon.aspx" name=".FormsAuthCookie"/></authentication>
The element loginUrl indicates the name of the login webpage, and name indicates the Cookie name.
(2) <authorization>
Purpose: control access to URL resources from clients (for example, Anonymous Users are allowed ). This element can be declared at any level (computer, site, application, subdirectory or page. Must be used with the <authentication> section.
For example, access by anonymous users is prohibited.
<authorization> <denyusers="?"/></authorization>
? Anonymous user,
* Indicates all users. You can also specify specific user prohibited.
(3) <system. web>
<Pages buffer = "true"/> All page configurations.
Buffer = "true": Enable server-side buffering. The server generates a complete page and sends it to the client.
Buffer = "false" indicates that Buffer is not enabled. The Buffer is sent continuously to the client in 16 kb.
<Etettings> custom configuration of the application. constants are generally placed, case sensitive. Create a storage database connection string.
<add key="con"value="server=.;database=Charge_sys;uid=sa;pwd=123456"></add>SqlConnection con=newSqlConnection(System.Configuration.ConfigtuationSettings.AppSettings["con"]
(4) <customErrors>
Purpose: Provide information about custom Errors for ASP. NET applications.
For example, when an error occurs, the webpage is redirected to the custom error page.
<customErrorsdefaultRedirect="ErrorPage.aspx"mode="RemoteOnly"></customErrors>
DefaultRedirect indicates the name of the custom error webpage. Mode element: displays custom (friendly) information for users not running on the Local Web server.
On indicates that both local and remote users will see custom error messages;
Off to disable custom error messages. both local and remote users will see detailed error messages.
RemoteOnly indicates that the local user will see the detailed error information, and the remote user will see the custom error information.
Summary:
Since there is a simple method, why do we have to go around? Since we can avoid repetition, we should try to achieve reusability. It can be said that the configuration file facilitates programming, and some of the easy-to-change things can be extracted without modifying the code. Just like in the data center charging system, we can flexibly change the database. You can flexibly connect to different databases. In fact, similar functions can be implemented in the parameter method, but when the connection instance is changed, the configuration file method modification is easier and safer than the parameter method.