If you prefer to use VS functions (for example, VS 2017), you can add environment variables on the Debug tab of the project properties. For example, in recent versions of ASP.NET Core (after RC2), you must set the ASPNETCORE_ENVIRONMENT variable.

As a result, the launchSettings.json file will be created (or updated) in the Properties folder of the corresponding project, so it will be easy to save this file in your original control solution and share it between developers (as opposed to other solutions with SET / SETX )
Note. By default, the latest ASP.NET Core sets up the environment in Production. Thus, you just need to set ASPNETCORE_ENVIRONMENT in Development in VS for debugging purposes (see screenshot above). And of course, if you want to run your code locally using the Staging environment, you must set ASPNETCORE_ENVIRONMENT to Staging . And finally, when you want to run it in a production environment, just delete this variable or set the Production value.
To summarize: just make sure that the Development, Staging, or Production values ββare used (not "Dev" or something else) in the Debug dialog box to set up your environment and create different extensions.
See also the corresponding source code from the ASP.NET kernel:
namespace Microsoft.AspNetCore.Hosting { /// <summary>Commonly used environment names.</summary> public static class EnvironmentName { public static readonly string Development = "Development"; public static readonly string Staging = "Staging"; public static readonly string Production = "Production"; } } namespace Microsoft.AspNetCore.Hosting { /// <summary> /// Extension methods for <see cref="T:Microsoft.AspNetCore.Hosting.IHostingEnvironment" />. /// </summary> public static class HostingEnvironmentExtensions { /// <summary> /// Checks if the current hosting environment name is "Development". /// </summary> /// <param name="hostingEnvironment">An instance of <see cref="T:Microsoft.AspNetCore.Hosting.IHostingEnvironment" />.</param> /// <returns>True if the environment name is "Development", otherwise false.</returns> public static bool IsDevelopment(this IHostingEnvironment hostingEnvironment) { if (hostingEnvironment == null) throw new ArgumentNullException("hostingEnvironment"); return hostingEnvironment.IsEnvironment(EnvironmentName.Development); } /// <summary> /// Checks if the current hosting environment name is "Staging". /// </summary> /// <param name="hostingEnvironment">An instance of <see cref="T:Microsoft.AspNetCore.Hosting.IHostingEnvironment" />.</param> /// <returns>True if the environment name is "Staging", otherwise false.</returns> public static bool IsStaging(this IHostingEnvironment hostingEnvironment) { if (hostingEnvironment == null) throw new ArgumentNullException("hostingEnvironment"); return hostingEnvironment.IsEnvironment(EnvironmentName.Staging); } /// <summary> /// Checks if the current hosting environment name is "Production". /// </summary> /// <param name="hostingEnvironment">An instance of <see cref="T:Microsoft.AspNetCore.Hosting.IHostingEnvironment" />.</param> /// <returns>True if the environment name is "Production", otherwise false.</returns> public static bool IsProduction(this IHostingEnvironment hostingEnvironment) { if (hostingEnvironment == null) throw new ArgumentNullException("hostingEnvironment"); return hostingEnvironment.IsEnvironment(EnvironmentName.Production); } /// <summary> /// Compares the current hosting environment name against the specified value. /// </summary> /// <param name="hostingEnvironment">An instance of <see cref="T:Microsoft.AspNetCore.Hosting.IHostingEnvironment" />.</param> /// <param name="environmentName">Environment name to validate against.</param> /// <returns>True if the specified name is the same as the current environment, otherwise false.</returns> public static bool IsEnvironment(this IHostingEnvironment hostingEnvironment, string environmentName) { if (hostingEnvironment == null) throw new ArgumentNullException("hostingEnvironment"); return string.Equals(hostingEnvironment.EnvironmentName, environmentName, StringComparison.OrdinalIgnoreCase); } } }
Evereq May 29 '17 at 9:40 pm 2017-05-29 21:40
source share