❔ Access AppSettings.json in asp.net core
I am receiving this error when attempting to access the AppSettings. It does not like the Configuration. What am I missing here?
20 Replies
Static methods can't access instance members
Do you need that method do be static?
If I remove the static it errors out on my call to method... I guess I don't really understand static... What should my syntax be here?
Why are you trying to call methods on a controller directly?
I am calling code for reusability...
Is this an ASP controller or does controller mean something different in this context, if it is, you probably want to split your querying out into a service
Then inject this service into the controller and the other place where you want to run this
You want your controllers to be thin interfaces that describe your API endpoints and then delegate to their services to do the actual work
The way it is now is code in one controller calling code in another controller...This works... I am just trying to get my Azure keys from AppSettings instead of hard coded in controller...
It doesn't work, hence the error message
If you really want to continue down this route, remove the static keyword from the method and inject the one controller into the other
It's absolutely not good practice to do so though
All your shared logic can live hapily in the service layer and these services can be reused by all your controllers / other services, it's a code smell for it to live in your controllers.
If you want an example of what I mean and can post the 2 controllers, I can show you how I'd refactor it
That would be awesome! Sorry for my ignorance here. Right now I have a Home Contoller and a NowSummary Controller. This code works . In the NOWSummary controller I have have
public async Task<ActiveStormKeyEntity> ExecuteActiveStormKeyTableQuery()
Here is some psuedo code from what you've said so far
BlazeBin - fiigzihkjcby
A tool for sharing your source code with the world!
StormKeyService would need to be registered as a scoped service in your configure services part of Program.cs or Startup.cs
Both controllers can inject the service and reuse the implementation of the
ExecuteActiveStormKeyTableQuery
methodThanks a bunch. Sorry for my ignorance. Just so I understand this... The Public Interface code should go where? In Visual Studio do I create a new interface and this will be a folder just like a controller?
I'd stick it in it's own file in the same folder as the implementation (the class that's "inheriting" from the interface)
When you say registered as a scoped service in Program.cs or startup.cs , could you elaborate on the code needed here...
Do you have one or both of those files?
Assuming you have a new world project then you should have something like
I have a startup.cs like this...
Just
services.AddScoped<IStormKeyService, StormKeyService>();
then in your ConfigureServices method, placement order doesn't matterThank you for all your help!! Much appreciated...
No problem, good luck with your project
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.