Appsettings.json environment: Setup files and use IWebHostEnvironment in .NET

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
different app settings.json files can be used for different asp.net core environments it's part three of our app settings.json course and we'll have a look at creating a new app settings.json file for a production environment remember to hit the red subscribe button or go to youtube.com around the code to subscribe to our YouTube channel learn.net dependency injection and blazer webassembly with round the coats online courses go to roundthecode.com courses by default on asp.net core web application will read of the app settings.json file now an asp.net core you can set up different environments built-in is the development staging and production we're going to create an app settings.json file now that is specific for the production environment I'm going to go ahead and create a new item and we're going to call it app settings dot production which is the environment not Json now we're going to add some settings in here and what this will do is that it will override the settings in the main app settings.json file so I'm going to call the authentication type and we're going to override the attempts so in the main file it's set as free but we're going to override it in production so it's set to four I'm also going to add a new app setting as well we're going to call it Live host and set the value as mindomain.com so what will happen is that if the asp.net core web app is running in production it will read the settings here first and use this as a precedence before reading the app settings.json file we're going to test this out in an asp.net core web API so we've got the home controller here we've got this index method and we're currently out pressing all the settings from the app settings.json we're going to add a new one of Live host we're expecting a string and it's just simply called Live host now in the main app settings.json this value doesn't appear here but it appears in the production if we run the application we can see that the Live host setting is returning null that's because in the launch settings it's set as development let's see what happens if we change it to production if we run the application we're getting the 404 not found that's because the API documentation that we're using called Swagger is only running in development environment so that's the reason but we can still call the API endpoint so we type in API home and this is returning our settings so you can see here that the attempts is different from the main app settings.json file and the Live host has been added as my domain so what's happening here is that because the application is running in a production environment it's reading the app settings.production environment and using these values first is then going back into the app settings.json and reading those values now obviously because we've already added attempts into the production it's reading the ones in production rather than the ones in the main app settings we can also read the environment in an asp.net core web API and to demonstrate this we're going to add it to the home controller so once again we set a private read-only member of a type of web host environment call it underscore environment and like the configuration it's added to the ioc container as part of the dependency injection so we can add it in as a parameter and we're going to call it environment then we're going to set the private member to the parameter that we're passing in next we're going to Output the environment name so to do that we're going to set a new key of environment then we're going to call our underscore environment instance now within the environment there's a lot of different members that we can use we can see whether the application is running development mode in staging or production or if we have our own custom environment for this though we're just going to Output the environment name so we're going to add that in and run the application running the application we can see that it's outputting the environment name as production in the next part of our app settings.json course we'll have a look at how login Works in appsettings.json to check out the full course playlist go to aroundthecode.com app settings hyphen course
Info
Channel: Round The Code
Views: 16,873
Rating: undefined out of 5
Keywords: .net core appsettings environment, appsettings environment, appsettings environment variables, ASP.NET Core appsettings environment, appsettings environment json, .NET Core appsettings environment variables, asp.net core change appsettings environment, appsettings environment override, appsettings environment files, appsettings development json, appsettings for different environments, create appsettings per environment
Id: BGtMYTx-G-Q
Channel Id: undefined
Length: 5min 11sec (311 seconds)
Published: Sun Oct 09 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.