How do i setup correct web.config in .Net Core 2.0

I have issue with configuring web.config in .Net Core 2.0. it’s not work.
I think maybe .Net Core 2.0 use appsetting.json instead of web.config
I have tried to add web.config to my project and use this code for register database connection

  var dbFactory = new OrmLiteConnectionFactory(
      ConfigUtils.GetConnectionString("agency_dev"), MySqlDialect.Provider);

And error is

ArgumentNullException: ConnectionString must be setParameter name: ConnectionString

You can still use appSettings in Web.config for your App config which is the default AppSettings provider, but you should be using the AppSettings API to read from Web.config or any other AppSettings source, e.g:

var connString = AppSettings.GetString("agency_dev");

.NET Core 2.0 has a convention that if your config file has this structure:

  "ConnectionStrings": {
    "MyCon": "data source=BLAHBLAH;"

then you can use their GetConnectionString helper method to get the connection string as @hoaihuongbk demonstrated above. As I understand it, calling AppSettings.GetString as you suggested would only return that key/value if it was on the top level of the config file (e.g. it maps to the index accessor on IConfiguration), but not nested under a ConnectionStrings category. Is my understanding correct? If so, what is the suggested way to get the connection string from the AppSettings in a .NET Core app?

I’d imagine like binding any other custom Poco as neither .NET Core’s IConfiguration API or ServiceStack’s IAppSetting has any knowledge or special APIs for retrieving connection string configurations. I’d personally just store it like any other string, there’s no reason it needs to be a special object that’s treated differently.

I believe .NET Core’s IConfiguration does have the special extension API of GetConnectionString. In any event, this works:


These are the main IConfiguration interfaces:

It’s not apart of the IConfiguration APIs, it’s just an extension method:

Hey @mythz

Was converting some old code to .Net Core and that old code was using ConfigUtils.GetConnectionString().

I ended up pulling some hairs as I couldn’t figure out why connectionString was empty. Then remembered AppSettings.GetString("ConnectionStrings:myconnstr").

Is ConfigUtils.GetConnectionString() going to be deprecated or made to work with .Net Core?

No the static ConfigUtils.GetConnectionString() can only work in ASP.NET.

For .NET Core you’d need an instance to the IConfiguration object which you can get injected in the Startup class:

public class Startup
    public Startup(IConfiguration configuration)
        Configuration = configuration;
    public IConfiguration Configuration { get; }

If you have access to ServiceStack’s AppSetting you can retrieve .NET Core’s underlying IConfiguration with:

var config = ((NetCoreAppSettings)AppSettngs).Configuration;