[英]IConfiguration is always NULL .NET CORE in Blazor app / Razor
IConfiguration is always null no matter what i try.. I have also tried to add singleton to the startup class, but always null...无论我尝试什么,IConfiguration 始终是 null。我还尝试将 singleton 添加到启动 class,但始终是 Z37A61DA78ECC04DFF9BDA9C
Startup class启动 class
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
Here is my Security class这是我的安全 class
IConfiguration _configuration { get;} **<<always null**
public Security(IConfiguration configuration) **<<always null**
{
_configuration = configuration;
}
public string GetConnectionString()
{
string connectionString = _configuration.GetConnectionString("localDb");
return connectionString;
}
Here is my Index.Razor page这是我的 Index.Razor 页面
namespace WebbiSkoolsQuizManager.Pages
{
public class IndexBase : ComponentBase
{
public IConfiguration Configuration { get; }
public async Task TryLogin(string user, string pass)
{
}
public async Task AddUserHash(string username, string password)
{
Security secure = new Security(Configuration);
string connectionstring = secure.GetConnectionString();
}
}
}
Appsettings.json应用设置.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"ConnectionKeys": {
"localDb": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=Quiz;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
},
"AllowedHosts": "*"
}
Thank you in advance, i have searched high and low, regarding a fix for blazor specific but i cant find anything.. (PS its.Net core 3)提前谢谢你,我已经搜索了高和低,关于 blazor 特定的修复,但我找不到任何东西..(PS its.Net core 3)
I guess this: public IConfiguration Configuration { get; }
我猜这个:
public IConfiguration Configuration { get; }
public IConfiguration Configuration { get; }
Should be:应该:
[Inject]
public IConfiguration Configuration { get; }
Note: You can't simply define a property of a service, and expect it to be populated... If you're using a Razor component class definition (.cs) as in your case, you can inject the service by using the [Inject]
attribute.注意:您不能简单地定义服务的属性,并期望它被填充...如果您使用的是 Razor 组件 class 定义(.cs),您可以使用
[Inject]
属性。 If you're using a Razor component (.razor), you can either use the @inject
directive at the view portion of the component, as for instance:如果您使用的是 Razor 组件 (.razor),您可以在组件的视图部分使用
@inject
指令,例如:
@page "/"
@inject IConfiguration Configuration
or in the @code
section, like this:或者在
@code
部分,像这样:
@code
{
[Inject]
public IConfiguration Configuration { get; }
}
Note also that if you want to inject a service into a normal C# class, you'll need to use constructor injection另请注意,如果要将服务注入普通的 C# class,则需要使用构造函数注入
I started a new blazor project (.Net 6) and when I @inject the IConfiguration to my.razor file, the configuration object turns out to be always null. I started a new blazor project (.Net 6) and when I @inject the IConfiguration to my.razor file, the configuration object turns out to be always null.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.