简体   繁体   English

在 Blazor 应用程序 / ZE405C1DF83BD248A07FC9A91227A

[英]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.

相关问题 IFormFile 始终为空(带有 MVC/Razor 的 ASP.NET Core) - IFormFile always null (ASP.NET Core with MVC/Razor) 将 IConfiguration 注入 .NET Core 3.0 中的 Program.cs(控制台应用程序) - Inject IConfiguration into Program.cs (Console App) in .NET Core 3.0 .NET Core - 为什么我应该使用视图组件而不是集成在 MVC 应用程序或 Razor 页面中的 Blazor 组件 - .NET Core - Why should I use View Components over Blazor Components integrated in MVC App or Razor Pages .NET Core DI 查看 IConfiguration - .NET Core DI looking into IConfiguration .NET IConfiguration为属性返回null - .NET IConfiguration returns null for a property 如何在 Blazor 服务器端 razor 组件中导入 IConfiguration? - How to import IConfiguration in Blazor server side razor component? ClaimTypes.NameIdentifier 在 Blazor WebAssembly ASP.NET 核心托管中始终返回 null - ClaimTypes.NameIdentifier returns always null in Blazor WebAssembly ASP.NET Core Hosted 在IAsyncPageFilter-Asp.Net Core 2.0 Razor Pages中的OnPageHandlerExecutionAsync中,PageResult始终返回null - PageResult always returns null in IAsyncPageFilter - OnPageHandlerExecutionAsync in Asp.Net Core 2.0 Razor Pages 净核心内联剃刀标记空模型 - Net Core Inline Razor Markup Null Model Json 连接字符串 - IConfiguration .net 核心 - Json Connection strings - IConfiguration .net core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM