如何以编程方式阅读web.配置值

本文关键字:web 配置 方式阅 编程 | 更新日期: 2023-09-27 18:07:09

我使用asp.net和c# 4。我有一个网站。配置文件

<globalization culture="auto:fr" uiCulture="fr"/>

我想在代码后面以编程方式在一个新变量中获得这个值。

var test = .......

如何获得文化价值?

解决方案:

Configuration config = WebConfigurationManager.OpenWebConfiguration("/");
GlobalizationSection section = (GlobalizationSection)config.GetSection("system.web/globalization");
OpenWebConfiguration("/"); // Point to Physical path for the Web.Config file (Useful when using Routing).
GetSection("system.web/globalization"); // Get the globalization section within the system.web node.

如何以编程方式阅读web.配置值

它是GlobalizationSection,所以你可以通过

var globalizationSection = 
        WebConfigurationManager.GetSection("globalization") as GlobalizationSection;

您可能需要导入系统。配置和System.Web.Configuration名称空间来执行此操作,但您可以这样做:

//and here is the code to get the section
Configuration config = WebConfigurationManager.OpenWebConfiguration("/");     
GlobalizationSection section = config.GetSection("globalization") as GlobalizationSection;

查看http://msdn.microsoft.com/en-us/library/system.web.configuration.globalizationsection.aspx

看起来你需要包含/使用System.Web.Configuration.GlobalizationSection

然后.Culturehttp://msdn.microsoft.com/en-us/library/system.web.configuration.globalizationsection.culture.aspx Y300