【问题标题】:Get a value from app settings in Blazor base class从 Blazor 基类中的应用设置中获取值
【发布时间】:2021-01-27 04:30:47
【问题描述】:

我有一个简单的 blazor 应用程序,它具有与 razor 页面不同的代码/基类。我试图从基类中的应用程序设置中获取一个值,但我不断收到错误消息。此错误在剃刀页面中。 我是否也必须在 razor 页面中传递一些东西?

"There is no argument given that corresponds to the required formal parameter 'iconfig' of 'ContactUsBase.ContactUsBase(IConfiguration)'

基类

public class ContactUsBase : ComponentBase
    {
        private IConfiguration _configuration;
        public ContactUsBase(IConfiguration iconfig)
        {
            _configuration = iconfig;
        }
        public ContactUs ContactUs { get; set; } = new ContactUs();

        protected async Task HandleValidSubmit()
        {
            var configurationValue = _configuration["myConfiguration"]; //THis is always null
        }

剃刀页面

@page "/contactus"
@inherits RealEstateWeb.BaseClasses.ContactUsBase

<div class="body-content">
 <EditForm Model="@ContactUs" OnValidSubmit="HandleValidSubmit">
</EditForm>
</div>

创业班

 services.AddSingleton(Configuration.GetSection("myConfiguration").Get<MyConfiguration>());

AppSettings.json

"myConfiguration": {
    "EmailDetails": {
      "Username": "test",
      "Password": "test"
    }
  }

【问题讨论】:

    标签: c# .net .net-core blazor blazor-server-side


    【解决方案1】:

    您不能在 Blazor 中使用构造函数注入。

    你应该像这样使用属性注入

    [Inject] public  IConfiguration Iconfig {get; set;}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-16
      • 1970-01-01
      • 2017-10-20
      • 1970-01-01
      • 1970-01-01
      • 2022-09-27
      • 2015-02-27
      • 2020-04-08
      相关资源
      最近更新 更多