How in ASP.NET 5 is this property defined?

5

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

So far so good, what I do not understand is how this property is defined. Why does it always have development value? How is this property really defined?

    
asked by anonymous 24.02.2015 / 00:11

2 answers

5

What you are looking for is in the code 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;
        }
    }
}

Then to change the property it is necessary to change the environment variable ASPNET_ENV in the operating system.

There's a discussion on this in GitHub .

    
24.02.2015 / 00:24
0

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

But to test your system in another development environment you do not necessarily have to set the OS environment variable, the Visual Studio tool already provides a way for you to set / change environment variables for your project, which is as follows:

Right-click on your project and then on Properties , your project's properties panel will pop up, go to the / Debug session and then you will see a table of properties called "Environment Variables ", which would be the environment variables for the project, would be some similar to this:

SototestyoucanchangethevalueofthepropertyHosting:EnvironmenttoProduction,sowhenrunningthesystemagainthevalueoftheEnvironmentNamepropertyoftheIHostingEnvironmentobjectwillbe"Production".

    
18.03.2016 / 18:31