DI ConnectionString
This commit is contained in:
@@ -7,6 +7,13 @@ namespace FahrzeugeMVC.Controllers;
|
||||
|
||||
public class FahrzeugController : Controller
|
||||
{
|
||||
private readonly IKonfigurationsleser _konfigurationsleser;
|
||||
|
||||
public FahrzeugController(IKonfigurationsleser konfigurationsleser)
|
||||
{
|
||||
_konfigurationsleser = konfigurationsleser;
|
||||
}
|
||||
|
||||
public IActionResult Index(string searchTerm)
|
||||
{
|
||||
string connectionString = this.GetConnectionString();
|
||||
@@ -29,7 +36,7 @@ public class FahrzeugController : Controller
|
||||
|
||||
public string GetConnectionString()
|
||||
{
|
||||
return "Server=localhost;User ID=admin;Password=admin;Database=FahrzeugDB";
|
||||
return _konfigurationsleser.LiesDBVerebindung();
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
|
||||
6
FahrzeugeMVC/Models/IKonfigurationsleser.cs
Normal file
6
FahrzeugeMVC/Models/IKonfigurationsleser.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace FahrzeugeMVC.Models;
|
||||
|
||||
public interface IKonfigurationsleser
|
||||
{
|
||||
string LiesDBVerebindung();
|
||||
}
|
||||
16
FahrzeugeMVC/Models/Konfigurationsleser.cs
Normal file
16
FahrzeugeMVC/Models/Konfigurationsleser.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
namespace FahrzeugeMVC.Models;
|
||||
|
||||
public class Konfigurationsleser : IKonfigurationsleser
|
||||
{
|
||||
private readonly IConfiguration _configuration;
|
||||
|
||||
public Konfigurationsleser(IConfiguration configuration)
|
||||
{
|
||||
this._configuration = configuration;
|
||||
}
|
||||
|
||||
public string LiesDBVerebindung()
|
||||
{
|
||||
return _configuration.GetConnectionString("MariaDB");
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,10 @@
|
||||
using FahrzeugeMVC.Models;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
builder.Services.AddControllersWithViews();
|
||||
builder.Services.AddScoped<IKonfigurationsleser, Konfigurationsleser>();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
|
||||
@@ -4,5 +4,8 @@
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"ConnectionStrings": {
|
||||
"MariaDB": "Server=localhost;User ID=admin;Password=admin;Database=FahrzeugDB"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,5 +5,8 @@
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
"AllowedHosts": "*",
|
||||
"ConnectionStrings": {
|
||||
"MariaDB": "Server=localhost;User ID=admin;Password=admin;Database=FahrzeugDB"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user