简体   繁体   中英

How do i get values from appsettings.json to BusinessLayer(my helper class) MVC 6

My controller which returns user details by calling an API intern

 public class HomeController : Controller
        {
            public ActionResult AccountDetails(int userId)
            {
                return this.Content(new WebHelperService().GetAccountDetails(userId)), "application/json");
            }
        }
  1. Here is my WebHelperService which is in Business Layer , where i need to get value from appsettings.json

      public class WebHelperService { private string url = null; public WebHelperService() { //url = ConfigurationManager.ConnectionString["ExternalApiUrl"].ToString(); // ConfigurationManager is not available in .net core. //So How do i read ExternalApiUrl from appsettings.josn,Which is the best way } public string GetAccountDetails(int userId) { return WebCall("{'userId':" + userId + "}"); } private string WebCall(string data) { WebRequest request = WebRequest.Create(url); // get the data from url and returns it } } 
    1. Do I need to carry settings all the way from controller in mvc6? Reference : docs.microsoft.com/en-us/aspnet/core/mvc/con.. )

Let's forget for a moment your particular use case and just talk about settings in .net core in general. Importantly, I think you are trying to access the raw AppSettings from your class, but what you actually want to do is DI them into your class. So let's do that.

Consider you have a appSettings.json that resembles something like below :

{
  "myConfiguration": {
    "myProperty": true 
  }
}

Now you need to create a POCO to hold these settings. Something like so :

public class MyConfiguration
{
    public bool MyProperty { get; set; }
}

In your startup.cs you should have a method called "ConfigureServices". In there you are going to place a call to "configure" your settings like so.

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<MyConfiguration>(Configuration.GetSection("myConfiguration"));
}

And so now you want to inject that settings object into a class. Let's call it MyClass for now. It would look like the following :

public class MyClass : IMyClass
{
    private readonly MyConfiguration _myConfiguration;

    public MyClass(IOptions<MyConfiguration> myConfiguration)
    {
        _myConfiguration = myConfiguration.Value;
    }
}

Now you have access to your configuration!

Bonus!

Instead you can make your ConfigureServices method look like the following :

public void ConfigureServices(IServiceCollection services)
{
    //services.Configure<MyConfiguration>(Configuration.GetSection("myConfiguration"));
    services.AddSingleton(Configuration.GetSection("myConfiguration").Get<MyConfiguration>());
}

What this now does is bind your services onto an actual class, not the IOptions object.

Now when you inject it into your class, you instead inject the POCO settings class, not IOptions. Like so :

public class MyClass : IMyClass
{
    private readonly MyConfiguration _myConfiguration;

    public MyClass(MyConfiguration myConfiguration)
    {
        _myConfiguration = myConfiguration;
    }
}

For further reading :

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