0

Well this shouldn't be this hard, I have a Class library package (new version from VS2015). Since I am referencing .Net 4.6 in the rest of the solution I removed the asp.Net5.4 completely from the project.json file for this library. I added the Configuration.Json package and the OptionsModel package as well, plus any dependencies. I am trying to follow some of the snippets I found on SO but none seem to get me anywhere.

MikesDotNetting has a good article on this but it only works in an mvc6 project OR a class library where you can reference MVC6 which doesnt work for me. http://www.mikesdotnetting.com/article/284/asp-net-5-configuration

 public class Startup
{
    public IConfiguration Configuration { get; set; }

    public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
    {
        var builder = new ConfigurationBuilder(appEnv.ApplicationBasePath)
            .AddJsonFile("config.json");

        Configuration = builder.Build();
    }
}

I see this in many of the examples, but ConfigurationBuilder takes an array of IConfigurationProviders, and appEnv.ApplicationBasePath is a string. So naturally this doesn't work here.

{
  "Data": {
    "DefaultConnection": {
      "ConnectionString": "Server=    (localdb)\\MSSQLLocalDB;Database=_CHANGE_ME;Trusted_Connection=True;"
    },
    "AppSetting": {
      "SMSProvider": "betterSms",
      "SMSAccountUser": "sakjfbhkdfbsdkfbksdfnbkdln",
      "SMSAccountSecret": "dswkjgfhszdkfhdskfhdk",
      "SMSPhoneFrom": "+15555555555"
    }
  }
}

I am simply trying to build a simple access mechanism to access the many appsettings contained in my config.json file.

dinotom
  • 4,990
  • 16
  • 71
  • 139

1 Answers1

0

You can access config variables in your app like:

var connString = Startup.Configuration.Get("Data:DefaultConnection:ConnectionString"); 
brettkc
  • 252
  • 1
  • 9