How in ASP.NET 5 is this property defined?

Asked

Viewed 174 times

6

In ASP.NET 5 in the method Configure class Startup we can receive in the parameters a reference to an object whose class implements IHostingEnvironment. One of the properties of this class is EnvironmentName. I have seen in example codes a check on this property to detail errors or not. If this property is Development then it comes to the development environment and shows up the errors in detail. Otherwise it is the production environment and shows a friendly page warning that an error has occurred.

So far so good, what I don’t understand is how this property is defined. Why does every time I run it have value "Development"? How this property is really defined?

2 answers

5


What you’re looking for is in the code of ConfigureHostingEnvironment.cs:

using System;
using Microsoft.Framework.ConfigurationModel;

namespace Microsoft.AspNet.Hosting
{
    internal class ConfigureHostingEnvironment : IConfigureHostingEnvironment
    {
        private IConfiguration _config;
        private const string EnvironmentKey = "ASPNET_ENV"; //esta é a variável de ambiente

        public ConfigureHostingEnvironment(IConfiguration config)
        {
            _config = config;
        }

        public void Configure(IHostingEnvironment hostingEnv)
        {
            hostingEnv.EnvironmentName = _config.Get(EnvironmentKey) ?? hostingEnv.EnvironmentName;
        }
    }
}

I put in the Github for future reference.

Then to change the property you need to change the environment variable ASPNET_ENV in the operating system.

There’s a discussion on this at Github.

0

What @Maniero said in your answer is correct! And that is internal process that is used to obtain the value of EnvironmentName.

But to test your system in another environment at development time you don’t necessarily have to set the OS environment variable, the Visual Studio tool already provides a way for you to configure/change environment variables for your project, which is the following:

Right-click on your project and then Properties, will appear the properties panel of your project, access the session/tab Debug And then you’re going to look at a property table called "Environment Variables," which would be the environment variables for the project, would be something similar to that:

inserir a descrição da imagem aqui

Then to test you can change the property value Hosting:Environment for Production, so when running the system again the value of the property EnvironmentName of the object IHostingEnvironment will be "Production".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.