简体   繁体   中英

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:)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM