How to calling appsettings.json from Controller
I've appsettings.json
This is HomeController.cs
namespace AppsCore.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
ViewBag.dsGrid = Employee.GetAll().ToArray();
ViewBag.dsDepartment = Department.GetAll().ToArray();
return View();
}
public ActionResult Insert([FromBody] CRUDModel<Employee> value)
{
var emp = value.value;
using (SqlConnection sql = new SqlConnection("Data Source=DESKTOP-5EFGK9O;Initial Catalog=Company;user id=dbCompany;password=6181;Connect Timeout=30;Encrypt=False;"))
//using (SqlConnection sql = new SqlConnection(_connectionString))
{
using (SqlCommand cmd = new SqlCommand("InsertEmployee", sql))
{
cmd.CommandType = System.Data.CommandType.StoredProcedure;
sql.Open();
cmd.Parameters.AddWithValue("@DepartmentId", emp.DepartmentId);
cmd.Parameters.AddWithValue("@Name", emp.EmployeeName);
cmd.Parameters.AddWithValue("@Designation", emp.Designation);
SqlParameter newSqlParam2 = new SqlParameter();
newSqlParam2.ParameterName = "@EmployeeId";
newSqlParam2.SqlDbType = SqlDbType.Int;
newSqlParam2.Direction = ParameterDirection.Output;
cmd.Parameters.Add(newSqlParam2);
cmd.ExecuteNonQuery();
emp.EmployeeId = (int)cmd.Parameters["@EmployeeId"].Value;
sql.Close();
}
}
ViewBag.dsGrid = Employee.GetAll().ToArray();
return Json(emp);
}
}
}
How to replace this with value from appsettings.json ?
using (SqlConnection sql = new SqlConnection("Data Source=DESKTOP-5EFGK9O;Initial Catalog=Company;user id=dbCompany;password=6181;Connect Timeout=30;Encrypt=False;"))
Please help. I'm stuck