简体   繁体   English

如何以编程方式读取web.config值?

[英]How to read programmatically web.config values?

I use asp.net and c# 4. I have a Web.Config file 我使用asp.net和c#4。我有一个Web.Config文件

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

I want to get this value in a new variable programmatically in Code Behind. 我想在Code Behind中以编程方式在新变量中获取此值。

var test = .......

How to get value for culture? 如何获得文化价值?

Solution Thanks to your Answers: 解决方案感谢您的回答:

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.

It's a GlobalizationSection , so you can get at it via 这是一个GlobalizationSection ,所以你可以通过它来获得它

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

You might need to import the System.Configuration and System.Web.Configuration namespaces to do this, but you can do something like this: 您可能需要导入System.Configuration和System.Web.Configuration命名空间才能执行此操作,但您可以执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM