Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a variable value in Web.config file

Tags:

asp.net-mvc

I have an C# MVC 5 app, which I intent to run in two places: a)Internet facing b) corporate Intranet I need to have my application code be aware of the environment (External Vs. Internal) in which it runs, so I can hide some features and options, which are not appropriate for the External instance. So, I'm thinking along the lines of creating some sort of distinct variable within my Web.config, to which I could assign either "external" or "internal" value.

My question is: what is the proper place and proper syntax to declare such variable, and where/how in my application C# code can I read that value, which should be read early enough in the app life cycle to give my JavaScript code (which is loaded in _layout.cshtml) a chance to act according to the "external"/"internal" value.

like image 397
Eugene Goldberg Avatar asked Dec 02 '25 00:12

Eugene Goldberg


1 Answers

You should put your value in appSettings, like:

<configuration>
    <appSettings>
        <add key="Environment" value="External" />
    </appSettings>
</configuration>

And you can pull it out like:

System.Configuration.ConfigurationManager.AppSettings["Environment"];
like image 169
Tom Avatar answered Dec 10 '25 00:12

Tom