ASP.NET Core MVC application dependency injection issue

  • Thread starter Thread starter Ravi Raghav
  • Start date Start date
R

Ravi Raghav

Guest
Hi all,

Recently i have tried to create a MVC application using ASP.NET Core 2.0 and i had some values defined in appsettings.json,

"MySettings": {
"WebApiBaseUrl": "http://localhost:6846/api/"
}

In order to read these values i have added

services.Configure<MySettingsModel>(Configuration.GetSection("MySettings"));


above line in ConfigureServices method in Startup.cs

and in my controller i have added

private readonly IOptions<MySettingsModel> appSettings;

public HomeController(IOptions<MySettingsModel> app)
{
appSettings = app;
}


MySettingsModel class is just a model with property same as key define in appsettings.json.
by this method i'm able to read the value of this key.

Now my issue is that i want to use this key in many controllers so i don't want to repeat this code in every controller so what i did was i created a BaseConntroller, added its constructor and i got my values there. But when i inherit other controllers with my BaseController , it throws me an error and tells me to generate it's constructor, so basically it tells me to add constructor in every controller which is what i wanted to avoid.

How can i achieve this?

This is the error that i get

1289018.png

And these are the potential fixes that it shows me

1289019.png

Continue reading...
 
Back
Top