Coder Social home page Coder Social logo

dotenv.net's Introduction

dotenv.net

CircleCI NuGet Badge License: MIT Book session on Codementor

dotenv.net is a zero-dependency module that loads environment variables from a .env file into Environment.

Contributors

Big ups to those who have contributed to this library. ๐Ÿ‘

@bolorundurowb @joliveros

Usage

Conventional

First install the library as a dependency in your application from nuget

Install-Package dotenv.net

or

dotnet add package dotenv.net

or for paket

paket add dotenv.net

Create a file with no filename and an extension of .env.

A sample .env file would look like this:

DB_HOST=localhost
DB_USER=root
DB_PASS=s1mpl3

in the Startup.cs file or as early as possible in your code add the following:

using DotEnv;


...


DotEnv.Config();

the values saved in your .env file would be avaibale in your application and can be accessed via

Environment.GetEnvironmentVariable("DB_HOST"); // would output 'localhost'

With ASP.NET Core or any other DI system that uses IServiceCollection

In the Startup.cs file

public void ConfigureServices(IServiceCollection services)
{
    ...

    // configure dotenv
    services.AddEnv(builder => {
        builder
        .AddEnvFile("/custom/path/to/your/env/vars")
        .AddThrowOnError(false)
        .AddEncoding(Encoding.ASCII);
    });
}

With this, your application would have the env file variables imported.

Options

ThrowError

Default: true

You can specify if you want the library to error out if any issue arises or fail silently.

DotEnv.Config(false); //fails silently

Path

Default: .env

You can specify a custom path if your file containing environment variables is named or located differently.

DotEnv.Config(true, "/custom/path/to/your/env/vars");

Encoding

Default: Encoding.Default

You may specify the encoding of your file containing environment variables using this option.

DotEnv.Config(true, ".env", Encoding.Unicode);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.