Khalid Ab
Khalid Ab

Reputation: 305

How to read appsetting.json from any class blazor serverside

i know how to acess to appsetting.json fron razor component but how from any class in a blazor serverside project?

from razor component i simply inject:

@inject IConfiguration _config

and access all that i need like : _config.GetConnectionString("default")

but how can do the same from any class? when i try to do

IConfiguration _config;

it says when i want to reach data that _config is null or is an unassigned local variable depands where i write the variable.

Upvotes: 4

Views: 22444

Answers (4)

valdessir
valdessir

Reputation: 21

In the constructor of your class, you could also have something like this if you don't want to pass IConfiguration as a parameter:

private IConfiguration _config 
public MyClass()
{
  _config = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
  _config.GetSection("YourSectionNameFromAppSettings.JSON").Value;
 }

Upvotes: 0

ECDev01
ECDev01

Reputation: 101

This is just a complement to @khalidAb and @KenTsu answers perhaps you find it useful. I usually work this way. Like KenTsu mentioned, it is just an implementation of Options pattern, he did it for a Blazor component, this is just for any class.

appSettings.json

"PositionOptions": {
    "Title": "Editor",
    "Name": "Joe Smith"
}

Model

public class PositionOptions
{
    public string Title { get; set; }
    public string Name { get; set; }
}

Startup.cs

services.Configure<PositionOptions>(Configuration.GetSection(nameof(PositionOptions)));

Any class

public class AnyClass {
    private readonly PositionOptions _positionOptions;

    public MyClass(IOptions<PositionOptions> positionOptions)
    {
        _positionOptions = positionOptions.Value;
    }

    public Get()
    {
        string title = _positionOptions.Title;
    }
}

Upvotes: 10

Khalid Ab
Khalid Ab

Reputation: 305

i understood how it work so i can answer my question myself

in fact, you just have to add a constructor in your class with Iconfiguration as argumentand you can use it and call your data stored in your app setteing:

    public class MyClass 
{
    public MyClass (IConfiguration config)
    {
        _config = config;
    }


    private readonly IConfiguration _config;
    


    void MyMethod(){
            .....
           }
}

You can use it after in your method:

string myCon = _config.GetConnectionString("Default");

Upvotes: 1

KennetsuR
KennetsuR

Reputation: 788

  • first, you should create custom config model
    public class PositionOptions
    {
        public const string Position = "Position";

        public string Title { get; set; }
        public string Name { get; set; }
    }
  • then, register config model in startup.cs
services.Configure<PositionOptions>(Configuration.GetSection(PositionOptions.Position));
  • don not forget appsetting.json
 "Position": {
    "Title": "Editor",
    "Name": "Joe Smith"
  }
  • at last, inject ioptions in the index.razor
@page "/"
@using WebApplication.Model
@using Microsoft.Extensions.Options
@inject IOptions<PositionOptions> _options
<h1>Hello, world!</h1>

Welcome to your new app.

<h1>@_options.Value.Title</h1>
<h1>@_options.Value.Name</h1>

<SurveyPrompt Title="How is Blazor working for you?"/>

Upvotes: 14

Related Questions