web.config File Usage

Global constants in web.config file and Usage in ASP.NET:

In the web.config file under <appSettings> tag declare the constants.
example:
 <appSettings>
      <add key="SqlConnection_local" value="Server=vijay-PC\SQLEXPRESS;Database=Student;User ID=vijay;password=123;Persist Security Info=True;"/><!--Ex: For Database Connection String-->
      <add key="clientURl" value="http://localhost:1555/YearBook/"/><!--Ex: For WebSite URI-->
      <add key="PI" value="3.14159265"/><!--Ex: For Mathematical Constant Values-->
 </appSettings>

How to use those variables inside ASP.Net:

string cURL = ConfigurationManager.AppSettings["clientURl"];
//to retrieve URL strings from webconfig global file.

float pi = float.Parse(ConfigurationManager.AppSettings["PI"]);
//to retrieve Constant Values

string conn= ConfigurationManager.AppSettings["SqlConnection_local"];
//to retrieve database connection strings

No comments:

Post a Comment