ciao!
mi sto intriducendo ad Asp net core mvc, e vorrei recuperare la connessione creata nel file di configurazione appsettings.json:
nel controller, giusto per capire se funziona o meno:codice:{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } }, "AllowedHosts": "*", "ConnectionStrings": { "ZaraConn": "Server=IP_ADDRESS;Database=NOME_DB;user id=USER;password=PWD;Trusted_Connection=True;MultipleActiveResultSets=true" } }
ma ottengo questo errore:codice:using System; using System.Diagnostics; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using CSharpNetCoreWeb.Models; using Microsoft.Extensions.Configuration; using System.Data.SqlClient; namespace CSharpNetCoreWeb.Controllers { public class HomeController : Controller { private readonly ILogger<HomeController> _logger; public IConfiguration Configuration { get; } public HomeController(ILogger<HomeController> logger) { _logger = logger; string connectionString = Configuration["ConnectionStrings:ZaraConn"]; using (SqlConnection connection = new SqlConnection(connectionString)) { string sql = "SELECT * FROM Richieste"; SqlCommand command = new SqlCommand(sql, connection); using (SqlDataReader dataReader = command.ExecuteReader()) { while (dataReader.Read()) { Console.WriteLine(dataReader[0]); } } connection.Close(); } } public IActionResult Index() { return View(); } public IActionResult Privacy() { return View(); } [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); } } }
qualche suggerimento??codice:System.NullReferenceException: 'Object reference not set to an instance of an object.' CSharpNetCoreWeb.Controllers.HomeController.Configuration.get ha restituito null.

Rispondi quotando