Configure ASP.NET Core with a .ENV file!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video i'll show you how to use the simple but powerful.net end package to add environment variables from a dot in file to your.net application and then configure them in asp.net core to use the configuration provider [Music] welcome back to our channel my name is chris roberts and this is robert's dev talk now there are a plethora of ways to configure an asp.net core or net core application of course the out of the box method is to use the app settings.json format now i find these files sometimes a little bit hard to handle and also i find that when storing secrets they don't play particularly nice with source control and also i do quite a lot of work with nest js which is node and laravel which is php as well they use dot n files to configure their environment during development so i went looking for a package in net that would do the same job and i found one it's called netent it's really simple to use here's how you do it so i have here a very simple.net 6 console project all it does is it pulls a message out of the environment variables and then it writes that message out to the console if i go to my.n file here i have my message which is subscribe now if i were to run this application now using.net run of course the message is blank because the console uploads nothing about my.n file so let's fix that let's use a dot net end package by installing.net add package dot net end and then if i head back to my program dot cs and i add the line dot net n dot n dot load and save that now if i clear my terminal and run dot net run again we'll see that dot net end has loaded the environment variables out of the dot n file and inserted them into the environment and my message now says robert's def talk says subscribe now what happens if i'm using a asp.net core template well let's find out now asp.net core is slightly more sophisticated because it already has a configuration provider built in the aggregates config from multiple different sources including environment variables so you want to work with that configuration provider so here i have a blank asp.net core mvc template i've already created a n file with a super secret connection string in which is foo db and i've also added my dot net n package already now if i go to my program.cs file notice under create host builder the create default builder already adds in the environment variables as a source so all i need to do is before this point run the dot net end dot end dot load method again now once i get to my startup i notice i have my eye configuration object injected in if i assign the value of my connection string from configuration and then super secret save that pop a breakpoint in and then run start debugging notice in my debug panel my secret is through db dot net end has loaded the environment variables from the dot n file and then asp.net core has picked up that environment variable and added it to our configuration now that's just a really quick introduction to the dot net end package there's quite a lot more you can do with it so i'll pop a link down in the description below to the documentation and the github repo i find it to be a really useful tool especially when moving between other frameworks like nest and laravel is great to use in development if you've liked this video then please do drop us a like and if you've got any questions about other packages or libraries you'd like us to cover then drop a comment down below as well if you like this kind of content then please do subscribe and also consider joining our community our developer journey where we started the newsletter actually and we're just going to once a month or so collate some of the interesting news and articles and tips that we've come across in our work and learning also we've got a link to buy me a coffee if you'd like to support what we do whether it's a coffee or a beer thank you so much for watching happy coding and we'll see you next time you
Info
Channel: Roberts Dev Talk
Views: 12,425
Rating: undefined out of 5
Keywords: developer, developers, code, coder, .net core, environment variables, asp.net core, dotnetenv, .env file in .net, .net env file, asp.net core tutorial, asp net core, dotnet core, asp.net core configuration environment variables, asp.net core configuration example
Id: lZOS27CwFT0
Channel Id: undefined
Length: 4min 30sec (270 seconds)
Published: Wed Jul 21 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.