Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm searching for a way to create a global constant variable, which I can use in my controllers.

I've totally no idea how to create that.

Thanks in advance

like image 326
ceuben Avatar asked Apr 22 '15 11:04

ceuben


People also ask

How do I add a global variable in ASP NET?

To get started in adding your static global variables in ASP.NET, create the App_Code folder. Then: Right-click on your project name and select "Add ASP.NET Folder" and then App_Code. Here is the code you can put in the class file. Static: The Global class is static.

What is the difference between global and static fields in ASP NET?

Global: Static fields and properties in ASP.NET are always global. Another copy is never created. You will not have duplication. Performance: Static fields are efficient in normal situations.

Why do we use static properties in ASP NET?

We use static properties (in the C# language) to store this single instance data. First, older versions of ASP used the Application [] object, but this is inefficient in ASP.NET. To get started in adding your static global variables in ASP.NET, create the App_Code folder.

How to access the field variable through the property?

The "get" and "set" keywords mean you can access the field variable through the property. Example 2. Here we want to use the static global variables in the class. Open your web page's code-behind file. It is usually named Default.aspx.cs. This web form accesses the global variable. And Page_Load first gets the static data.


Video Answer


2 Answers

Very comfortable way is to use Strongly-Typed Settings for that. You can access to these variables everywhere in a project and change its values without recompilation.

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

Settings VS Editor

These variables will be added to an appropriate section in a Web.config or App.config file in this way:

<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 defined variables anywhere in your project in a simple way:

string myStringVariable = Settings.Default.SomeStringVariable;
bool myBoolVarialbe = Settings.Default.SomeBoolVariable;
double myDoubleVariable = Settings.Default.SomeDoubleVariable;
like image 111
Kryszal Avatar answered Oct 05 '22 23:10

Kryszal


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

set the property as

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

accesses it anywhere

Constant.YourConstant;

or 2. you can also use web.config

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

Use it as

ConfigurationManager.AppSettings["YourConstant"];
like image 43
Debasish Avatar answered Oct 05 '22 23:10

Debasish