Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

How to import IConfiguration in Blazor server side razor component?

$
0
0

I have a razor component that I want to use a configuration value on, from my appsettings.json file, and I've followed the example here: Inject an IConfiguration

But that doesn't work inside the @code block for me.

My razor component looks like this so far:

@using Microsoft.Extensions.Configuration@inject IConfiguration Configuration@code {    private string strValue = Configuration.GetSection("MySection").Value;}

I get the following error on the Configuration.GetSection line:

A field initializer cannot reference the non-static field, method, orproperty 'MyComponent.Configuration'

I can apparently use @Configuration outside of the @code section without error.

Am I missing something? I wasn't able to find a post relating to this exact issue, so sorry if this is a duplicate.


Viewing all articles
Browse latest Browse all 4839

Trending Articles