How to create global constant variables in asp.net MVC 5

I am looking for a way to create a global constant variable that I can use in my controllers.

I do not know how to create this.

Thanks in advance

+5
source share
2 answers

A very convenient way is to use Strong settings . You can access these variables everywhere in the project and change its values ​​without recompiling.

You can use the Visual Studio editor to define settings (Project> Properties> Settings):

Settings VS Editor

These variables will be added to the appropriate section in the Web.config or App.config file as follows:

<setting name="SomeStringVariable" serializeAs="String"> <value>SomeStringValue</value> </setting> <setting name="SomeBoolVariable" serializeAs="String"> <value>false</value> </setting> <setting name="SomeDoubleVariable" serializeAs="String"> <value>1.23</value> </setting> 

You can use certain variables anywhere in your project in a simple way:

 string myStringVariable = Settings.Default.SomeStringVariable; bool myBoolVarialbe = Settings.Default.SomeBoolVariable; double myDoubleVariable = Settings.Default.SomeDoubleVariable; 
+14
source

1: generate a static class (say Constant.cs)

set the property as

 public static string YourConstant{ get { return "YourConstantValue";}} 

refers to him anywhere

 Constant.YourConstant; 

or 2. you can also use web.config

 <appSettings><add key="YourConstant" value="YourConstantValue" /></appSettings> 

Use it like

 ConfigurationManager.AppSettings["YourConstant"]; 
+9
source

All Articles