6

I use asp.net and c# 4. I have a Web.Config file

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

I want to get this value in a new variable programmatically in 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.
0

3 Answers 3

8

It's a GlobalizationSection, so you can get at it via

var globalizationSection = 
        WebConfigurationManager.GetSection("globalization") as GlobalizationSection;
Sign up to request clarification or add additional context in comments.

8 Comments

I tired with debugger and I get globalizationSection = null ... any ideas?
@GibboK - can you add the code that you are using for this to your question?
i'm using your code. var globalizationSection = WebConfigurationManager.GetSection("globalization") as GlobalizationSection;
in web.config <system.web> <globalization culture="auto:fr" uiCulture="fr"/>
@GibboK - please add the complete relevant code you are using. Are you referencing System.Configuration assembly in the project? Does you code compile?
|
3

You might need to import the System.Configuration and System.Web.Configuration namespaces to do this, but you can do something like this:

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

5 Comments

Cannot get Configuration DataType using the namespace using System.Web.Configuration; ... what I'm missing. thanks
@GibboK: Try importing System.Configuration too.
I try too but the problem persist, please read my comment to Russ Cam, I still have a strange problem with this. Thanks.
@GibboK: Add System.Configuration as a reference to the project, and then import System.Configuration and System.Web.Configuration on the page you need it.
Try - var globalizationSection = config.GetSection("system.web/globalization") as GlobalizationSection;
0

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

It looks like you need to include/use System.Web.Configuration.GlobalizationSection

Then .Culture http://msdn.microsoft.com/en-us/library/system.web.configuration.globalizationsection.culture.aspx#Y300

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.