我有我的「appsettings.json」文件內容ASP.Net DI使用類控制器內沒有實例化類控制器內
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},
"appData": {
"applicationDeveloper": "El Bayames"
}
}
以及從AppSettings的閱讀,更
public class learningDIGlobalVariables
{
private String _applicationDeveloper;
private String _webRootFolderPath;
public String applicationDeveloper
{
get
{
return _applicationDeveloper;
}
}
public String webRootFolderPath
{
get
{
return _webRootFolderPath;
}
}
public learningDIGlobalVariables(IConfigurationRoot auxConfRoot, Microsoft.AspNetCore.Hosting.IHostingEnvironment hostingEnvironment)
{
_applicationDeveloper = auxConfRoot["appData:applicationDeveloper"];
_webRootFolderPath = hostingEnvironment.WebRootPath;
}
}
類
而設置的容器內,我有
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
services.AddSingleton<IConfigurationRoot>(Configuration);
}
內的控制器我有
public class HomeController : Controller
{
private learningDIGlobalVariables _learningDIGlobalVariables;
public HomeController(IConfigurationRoot auxConfRoot, Microsoft.AspNetCore.Hosting.IHostingEnvironment hostingEnvironment)
{
_learningDIGlobalVariables = new learningDIGlobalVariables(auxConfRoot, hostingEnvironment);
}
}
如何在控制器內使用該類「learningDIGlobalVariables」而不實例化它?這個類沒有被框架實例化嗎?如果我必須將該類添加到服務中,我該怎麼做並在後者上使用它?
而且,控制器如何在控制器內使用它? –
它是公共類,所以你可以使用你現在使用它的方式,除了你不必初始化它。 – anserk