Wednesday, 27 February 2013

ASP.NET:Set Constants In Web.Config File And Access Them In Code File

Sometimes we need to use the constants in different parts of our project.  We can simply use constant values. But what if after a span of time (months or years) the constant value changes?  Then the programmer need to change the constant value on every place where he had used the constant.
 One solution to this problem is to set the value of constant in web.config file in the <appSettings> tag.
Then we can access this value at code file wherever we require.Let me explain it with example.

Put the following code inside your web.config file:-

ASP.NET:Set Constants In Web.Config File And Access Them In Code File

Now to access this values inside the code file write the following code:-

ASP.NET:Set Constants In Web.Config File And Access Them In Code File

Now wherever you use the value1 and value2 values will be changed whenever you change the value attribute of the key inside <appSettings>   tag of the web.config file. You can place the keys and values in separate file and reference it in web.config file Click Here for code.

1 comment:

WooCommerce an WordPress open-source e-commerce plugin

WooCommerce is an WordPress open-source e-commerce plugin. WooCommerce is a free WordPress plugin that have e-commerce functionality to your...