Hello friends, I hope you all are doing great. In today's tutorial, we will have a look at How to use Environment Variables in ASP.NET Core. It's our 5th tutorial in ASP.NET Core series. In our Startup.cs file, you must have seen that we have an IF Condition at the start and this IF Condition has IsDevelopment in it.
This condition is actually checking the environment of our web application. So, here we will first discuss them and then will see why we need them. So, let's get started:
Environment Variables in ASP.NET Core
ASP.NET Core has 3 builtin Environments, which decides the Run-time operating environment of the web application, which are:
Development.
Staging.
Production.
In order to set this Environment Variable, we need to open the launchSettings.json file, in the Properties section of SolutionExplorer.
This launchSettings.json file has two profiles in it and both of these profiles have a variable ASPNETCORE_ENVIRONMENT, this variable is used to set the Environment value.
In the above code, you can see that we have set the value of ASPNETCORE_ENVIRONMENT to be Development, that's why we are in Development mode.
Let's discuss these 3 environment variables one by one:
Development Environment
As the name implies, when we are developing the application then we are in Development Environment, so the value of this variable should be Development.
In Development mode, we can show many details for understanding which we can't directly show to our end user.
At the start of this Configure Method, you must have seen IsDevelopment, its a builtin ASP.NET Core Boolean function, which is checking the Environment.
If the Environment is Development then show the Developers Exception Page and as this page has a lot of useful information, we can't show this information to our user, it will be dangerous.
That's why, we have placed this check that if we are in the Development Environment, only then show this Exception Page. ( We will discuss Exception Page later in detail )
Staging Environment
When you are developing complex cloud platform web application, then before transferring it to Production Environment, you first have to test it out in Staging Environment.
Staging Environment is quite equivalent to Production Environment and is normally for QA purposes to have a final look before publishing.
You can also use IsStaging function just like IsDevelopment in ASP.NET Core.
Production Environment
When you are fully confident that your application is complete and follows all security protocols etc. then you can set the Environment to Production.
If you remove this ASPNETCORE_ENVIRONMENT code line from the lauchSettings.json file, then by default, the runtime Environment is Production.
We also have IsProduction function to use in ASP.NET Core.
Custom Environment
We can also create a new custom environment in ASP.NET Core, we just need to specify this new name i.e. TEP in the launchSettings.json file.
Now, in order to check the custom environment, we need to use IsEnvironment("TEP") function, as shown in the code on right side.
We can use any name for this custom environment, we need to use custom environments in such application where we need to use separate profiles i.e. teacher, student, parents etc.
Developer Exception Page Middleware
As we have discussed the Environments, so now let's have a look at this Middleware Developer Exception Page.
It's another builtin ASP.NET Core Middleware, which shows an Exception Page to the Developer, if there's any exception occurs in your application.
This exception page is very helpful, as it gives detailed information about the exception or the issue occurred in the application.
We don't want to show this Exception Page to our end user as it reveals a lot of information related to our app, which could be helpful in hacking stuff.
So, that's why we have placed it inside IsDevelopment condition.
So, that was all about Environment Variables in ASP.NET Core. In the next tutorial, we will have a look at MVC architecture in ASP.NET Core. Till then take care & have fun !!! :)
syedzainnasir
I am Syed Zain Nasir, the founder of The Engineering Projects (TEP). I am a
programmer since 2009 before that I just search things, make small projects and now I am sharing my
knowledge through this platform. I also work as a freelancer and did many projects related to
programming and electrical circuitry. My Google Profile+Follow
Get Connected
Comments on ‘’ Environment Variables in ASP.NET Core ‘’ ( 0 )