Tray Simpson
Tray Simpson

Reputation: 11

Injecting IOptions config always returning null in .razor file

I'm fairly new to .NET and am working on an existing project. I've setup a configuration class that works perfectly when injecting into a service like so:

public PayPalConnection(IOptions<PayPalConfiguration> config) { 
   DoSomething(config.Value.SomeValue);
}

However, I'm having some trouble getting it to work on my Razor pages. I would expect this to work (given that I can access SomeValue on my service without issue):

@inject IOptions<PayPalConfiguration> Configuration;
<div> @Configuration.Value.SomeValue</div>

But this always returns null. I have also tried things like

@inject IConfiguration Config
<div>@Config["PayPal:SomeValue"]</div>

But that too returns nothing. I've tried with other configuration classes that are setup, and it seems that any configuration I try to use in my razor pages is always null. Any recommendations are much appreciated :)

Upvotes: 0

Views: 452

Answers (1)

Tray Simpson
Tray Simpson

Reputation: 11

Thanks to Henk Holterman I realized I was trying to access the configuration of my Web (Server) Solution from my WebClient Solution so it was returning null. I fixed this by using an HttpClient to fetch what I needed from the Web Solution and that resolved my issue.

Upvotes: 1

Related Questions