Create a custom configuration section in ASP.NET (translation)

zhaozj2021-02-16  87

First, introduction

ASP.NET web applications access simple "Key / Value" configuration data with a built-in method. In the web.config file, you can create

Section to store simple "keys / value" pairs. For example, create a new ASP.NET project, add the following in the web.config file

Tag

Child tag of element:

This section contains two

Tag definition "Key / Value" pair, you can get their value with the ConfigurationSettings property built into the PAGE object. As a beginning, create a web form called CustomItems.aspx in your project, add the following code to the Page_Load event of the form:

Dim akey as string

Response.write (""

Appsettings ")

For Each Akey in ConfigurationSettings.Appsettings.Keys

Response.Output.writeline (akey & "=" & _

ConfigurationSettings.AppSettings.Item (akey))

NEXT

Compile the CustomItems.aspx web form, you can see it.

The value of the tag is. For loop retrieval

Infinite

Tag and display the key and its corresponding attribute value. This simple "key / value" mechanism is perfect for many general demand, such as storeing database connection strings throughout the application, but for more complex data is not enough. Fortunately, Microsoft also establishes a mechanism for creating custom configuration data, using an ASP.NET framework to read one or more

Instead, not only to read the fixed tag list through the code in a specific application.

The section defines the framework that is expected to be discovered in the rest of the web.config file, and declare the type and location of the class that handles its particular type of content.

When parsing the configuration file, the ASP.NET engine is read by reading.

Elements

Mark establishes a list of possible tags, each of which

The tag contains a ""

Name

"and a"

Type

", Declare that the expected marker name and corresponding configuration section processing program in the rest of the file. The following uses a small experiment to demonstrate the entire work process. In the project

Web.comfig

Document

The front of the mark is added, and a new tag is added as follows.

Save the web.config file and run the project, will get an error in "Unrecognized configuration section 'CustomItems'", this error occurs because there is no statement

The tagged section is caused. But if you browse the entire web.config file, you will not see any of the markup section processing programs declaration, which brings a problem. Where is these configuration sections declared? (When reading this article, if you follow the above steps, please continue

The tag is deleted from the web.config file. )

In fact, every web application has two profiles: Save the root machine.config file under the system folder and the web.config file in your application root directory. You can be able under the operating system folder / Microsoft.Net/framework/

/ Config

Find the Machine.config file in the folder, where

The .NET framework corresponding to the server is installed and activated. Machine.config files About configuration settings apply to all applications on the server unless they are reset by local settings. Browse the entire Machine.config file, you can see a set of groups.

marked

Mark, these

The tag declares that you can

Web.config

Configuration section handles that are seen in the files. In order to make this process easier to understand, it can be further

Mark packet

In the tag, a set of related section marks are stored separately.

The reason why I will introduce a Machine.config file because there are two ways to add a custom tag: You can use any default system configuration segment handler to resolve custom tag content, or create your own configuration segment processing program. .

Second, use the system configuration section handler to resolve custom tags

In

Element creates a new one

Mark, as follows:

type = "System.Configuration.NameValueSectionHandler, System, Version = 1.0.3300.0, Culture = neutral, PublicKeyToken = b77a5c561934e089" /> authors caution: Version and PublicKeyToken value and you may be different versions of the .NET Framework, to find in the system The correct value is only necessary for any existing

Copying in the element!

2. Will be new

Test before the end tag in the web.config file, for example:

Key = "somekey" value = "somevalue" />

3. Save the web.config file, add the following highlights to the Page_Load event in the CustomItems.aspx web form: Dim Akey as string response.write ("appsettings") for each akey in configurationSettings.Appsettings.keys Response .Output.writeline (akey & "=" & _ configurationSettings.appsettings.item (akey)) Next Response.write ("CustomsystemItems")

For Each Akey In CType (ConfigurationSettings.getconfig_

("CustomsystemItems"), _

System.collections.specialized.namevaluecollection .keys

Response.Output.writeline (akey & "=" & _

ConfigurationSettings.AppSettings.Item (akey))

NEXT

4. Now compile the Web form again. This time, you can see that the CustomsystemItem header information has a line of "somekey = somevalue", which corresponds to

An increase in elements

Child elements. By modifying the Machine.config file, you can apply the defined custom tag to any web application that runs on the server. But it is likely that you don't always want the tag handler to apply to all applications. If so, you can add it in the web.config file.

Tag

Tag, not

Machine.config

Document. Test it, first delete it

Machine.config

Defined in the file

Mark and save, then

Web.config

The file is tightly followed by the start mark

add one

Mark and place it in it

mark. E.g:

Type = "system.configuration.namevaluesectionHandler, System, Version = 1.0.3300.0, Culture = NEUTRAL, PUBLICKEYTOKEN = B77A5C561934E089" /> 5. In order to change, in the web.config file

Square additional one

mark.

Key = "somekey" value = "somevalue" />

Key = "anotherkey" value = "anothervalue" />

Save the web.config file and run the CustomItems.aspx web form again, you can see two values ​​and no longer just one. You don't have to recompile the application to complete the test, ASP.NET can apply new configurations immediately. Using this method can define any number of custom tags. However, use ordinary

Mark, and "key" and "value" fixed attribute names, always seek not particularly intuitive. Considering the maintenanceability, creating custom tags that can simultaneously control tags and attribute names will be more efficient.

Third, define the custom configuration section processing program for parsing custom tags

Assume that a series of articles need to be defined, each article has a title, a URL, no or multiple authors, then the marking structure shown below is more common.

More easily maintained:

URL = "http://www.somewhere.com/Article1.aspx">

Russell Jones

转载请注明原文地址:https://www.9cbs.com/read-11632.html

New Post(0)