Get a value from app settings in Blazor base class

Viewed 293

I have a simple blazor app which has separate code/base classes from the razor pages. I am trying to get a value from app settings in the base class but I keep getting an error. This error is in the razor page. Do I have to pass something in the razor page as well?

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

Base Class

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
        }

Razor Page

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

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

Start Up Class

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

AppSettings.json

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

You can't use constructor injection in Blazor.

You should use property injection like this

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