在配置文件应用于 Web 应用程序时提供对配置文件的访问。
程序集: System.Web(在 System.Web.dll 中)
WebConfigurationManager 类型公开以下成员。
名称 | 说明 |
---|---|
AppSettings | 获取该网站的应用程序设置。 |
ConnectionStrings | 获取网站的连接字符串。 |
使用 WebConfigurationManager 类可以访问计算机和应用程序信息。
使用 WebConfigurationManager 是处理与 Web 应用程序相关的配置文件的首选方法。 对于客户端应用程序,请使用 ConfigurationManager 类。
您的应用程序可以扩展 System.Configuration 类型或直接使用它们来处理配置信息,如下面的列表中所述。
-
Handling configuration . 若要使用标准类型处理配置信息,请使用下列方法之一:
-
Accessing a section . 若要访问应用程序的配置信息,必须使用 WebConfigurationManager 提供的 GetSection 方法之一。 对于 <appSettings> 和 <connectionStrings>,可以使用 AppSettings 和 ConnectionStrings 属性。 上面提到的方法执行只读操作,使用配置的单个缓存实例,并且可识别多线程。
-
Accessing configuration files . 应用程序能够读写任何级别的配置设置,不管是自己的还是其他应用程序或计算机的,也不管是本地的还是远程的。 可以使用WebConfigurationManager 提供的 open 方法之一。 这些方法将返回 Configuration 对象,该对象又提供处理基础配置文件所需的方法和属性。 这些方法执行读取或写入操作,而且每次打开文件时都会重新创建配置数据。
-
Advanced configuration . 以下类型提供了更高级的配置处理方法:SectionInformation、PropertyInformation、PropertyInformationCollection、ElementInformation、ContextInformation、ConfigurationSectionGroup 和ConfigurationSectionGroupCollection。
-
-
Extending configuration standard types . 通过使用编程模型或特性化模型扩展标准配置类型(如 ConfigurationElement、ConfigurationElementCollection、ConfigurationProperty和 ConfigurationSection),您还可以提供自定义的配置元素。 有关如何以编程方式扩展标准的配置类型的示例,请参考 ConfigurationSection 类。 有关如何以特性化模型扩展标准的配置类型的示例,请参考 ConfigurationElement 类。
Configuration 类允许进行编程访问以编辑配置文件。 可以使用 WebConfigurationManager 提供的 open 方法之一。 这些方法将返回 Configuration 对象,该对象又提供处理基础配置文件所需的方法和属性。 可以按如下方式访问这些文件以进行读取或写入。
使用 GetSection 或 GetSectionGroup 读取配置信息。 请注意,进行读取操作的用户或进程必须具有以下权限:
-
在当前配置层次结构级别下对配置文件的读取权限。
-
对所有父级配置文件进行读取的权限。
如果您的应用程序需要对其自身的配置进行只读访问,则建议您使用 GetSection 方法。 这些方法提供对当前应用程序的缓存配置值的访问;与 Configuration 类相比,这样做可以提供更好的性能。
![]() |
---|
如果使用采用 path 参数的静态 GetSection 方法,则该路径参数必须引用正在其中运行代码的应用程序,否则将忽略该参数并返回当前运行的应用程序的配置信息。 |
使用 Save 方法之一写入配置信息。 请注意,进行写入操作的用户或进程必须具有以下权限:
-
对配置层次结构中当前级别的配置文件和目录的写入权限。
-
对所有配置文件的读取权限。
Topic | Location |
---|---|
如何:以编程方式访问 ASP.NET 配置设置 | 配置 ASP .NET Web 应用程序 |
如何:锁定 ASP.NET 配置设置 | 配置 ASP .NET Web 应用程序 |
如何:以编程方式查看继承的配置设置和本地配置设置 | 配置 ASP .NET Web 应用程序 |
如何:以编程方式访问 ASP.NET 配置设置 | 在 Visual Studio 中生成 ASP .NET Web 应用程序 |
如何:锁定 ASP.NET 配置设置 | 在 Visual Studio 中生成 ASP .NET Web 应用程序 |
如何:以编程方式查看继承的配置设置和本地配置设置 | 在 Visual Studio 中生成 ASP .NET Web 应用程序 |
下面的示例演示如何使用 访问配置信息。
GetSection 方法。
// Show how to use the GetSection(string). // to access the connectionStrings section. static void GetConnectionStringsSection() { // Get the connectionStrings section. ConnectionStringsSection connectionStringsSection = WebConfigurationManager.GetSection("connectionStrings") as ConnectionStringsSection; // Get the connectionStrings key,value pairs collection. ConnectionStringSettingsCollection connectionStrings = connectionStringsSection.ConnectionStrings; // Get the collection enumerator. IEnumerator connectionStringsEnum = connectionStrings.GetEnumerator(); // Loop through the collection and // display the connectionStrings key, value pairs. int i = 0; Console.WriteLine("[Display the connectionStrings]"); while (connectionStringsEnum.MoveNext()) { string name = connectionStrings[i].Name; Console.WriteLine("Name: {0} Value: {1}", name, connectionStrings[name]); i += 1; } Console.WriteLine(); }