Read Connection String from AppSettings.json file in ASP.Net Core MVC

wkm1925
 
on Jul 14, 2021 04:32 AM
716 Views

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

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Jul 14, 2021 04:32 AM
wkm1925
 
on Jul 14, 2021 07:18 AM

I get this,

See this,

    public class HomeController : Controller
    {
        private IConfiguration Configuration;

        public HomeController(IConfiguration _configuration)
        {
            Configuration = _configuration;
        }

And this,

        public IActionResult Index()
        {
            string ConnStr = this.Configuration.GetConnectionString("defaultConnection");

            ViewBag.dsGrid = Employee.GetAll(ConnStr).ToArray();
            ViewBag.dsDepartment = Department.GetAll(ConnStr).ToArray();
            return View();
        }

And this,

            public static List<Employee> GetAll(string ConnStr)
            {               
                if (emp.Count() == 0)
                {
                    //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(ConnStr))
                    {
                        using (SqlCommand cmd = new SqlCommand("getEmployee", sql))
                        {
                            cmd.CommandType = System.Data.CommandType.StoredProcedure;
                            sql.Open();

                            using (var reader = cmd.ExecuteReader())
                            {
                                while (reader.Read())
                                {
                                    emp.Add(new Employee(

                                            (int)reader["Id"],
                                            (int)reader["DepartmentId"],
                                            reader["DepartmentName"].ToString(),
                                            reader["Name"].ToString(),
                                            reader["Designation"].ToString()
                                        ));
                                }
                            }

                            sql.Close();
                        }
                    }

                }
                return emp;
            }