How to read data from appsetting file?

Forums .NET CoreHow to read data from appsetting file?
Staff asked 2 months ago

Answers (2)

Add Answer
Umang Ramani Marked As Accepted
Staff answered 2 months ago

Add dependency :

public UserController(IConfiguration iConfig)
{
    configuration = iConfig;
}

 

Get Value :

string dbConn = configuration.GetSection("MySettings").GetSection("DbConnection").Value;

 

Staff answered 2 months ago

Use IConfiguration  interface to read value from appsetting.

Example:

Appsetting.json
"MySettings": {  
    "Email": "abc@domain.com",  
    "SMTPPort": "5605"  
  }  

private IConfiguration  configuration ;
public TestController(IConfiguration iConfig)  
{  
 configuration = iConfig;  
} 
string MyValue= configuration.GetSection("MySettings").GetSection("Email").Value;

 

 

 

Subscribe

Select Categories