在.NET Core 2.0中设置环境变量

编程入门 行业动态 更新时间:2024-10-26 06:26:36
本文介绍了在.NET Core 2.0中设置环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试在.NET Core 2.0应用程序中设置多个环境.请参阅下面的代码.

I am trying to setting up multiple environments in my .NET Core 2.0 application. See my code below.

"configurations": [ { "name": ".NET Core Launch (web)", "type": "coreclr", "request": "launch", "preLaunchTask": "build", // If you have changed target frameworks, make sure to update the program path. "program": "${workspaceRoot}/my.api/bin/Debug/netcoreapp2.0/my.api.dll", "args": [], "cwd": "${workspaceRoot}/my.api", "stopAtEntry": false, "requireExactSource": false, "internalConsoleOptions": "openOnSessionStart", "launchBrowser": { "enabled": true, "args": "${auto-detect-url}", "windows": { "command": "cmd.exe", "args": "/C start ${auto-detect-url}" }, "osx": { "command": "open" }, "linux": { "command": "xdg-open" } }, "env": { "ASPNETCORE_ENVIRONMENT": "Development" }, "sourceFileMap": { "/Views": "${workspaceRoot}/Views" } }, { "name": ".NET Core Attach", "type": "coreclr", "request": "attach", "processId": "${command:pickProcess}" } ]

Program.cs

public class Program { public static void Main(string[] args) { BuildWebHost(args).Run(); } public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>() .Build(); }

StartUp.cs

public class Startup { public IContainer ApplicationContainer { get; private set; } private IHostingEnvironment HostingEnvironment { get; set; } public IConfigurationRoot Configuration { get; } private string ConnectionString { get { return this.HostingEnvironment.IsDevelopment() ? Configuration.GetConnectionString("DefaultConnection") : Configuration.GetConnectionString("Production"); } } public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.Development.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.Azuredev.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables(); Configuration = builder.Build(); this.HostingEnvironment = env; System.Console.WriteLine(env.EnvironmentName); // Here it always give me Production. }

我的问题

我尝试使用 dotnet run --environment"Development"

因此,它应该在开发环境上运行,但始终与生产一起运行,(看起来我已经在我的 console.writeline 中添加了它) startup.cs 文件)

So, it should run on Development Environment, but it always runs with Production, (look I have added console.writeline in my startup.cs file)

现在奇怪的是,如果我使用 F5 进行调试,那么它可以在 development 环境中完美运行.

Now the strange thing is that if I use F5 to Debug then it runs perfectly with the development environment.

推荐答案

您可以更新launchsettings.json以包含开发"配置文件,然后运行:

You can update your launchsettings.json to include a 'Development' profile and then run:

dotnet run --launch-profile "Development"

有关launchSettings.json文件配置的更多详细信息,请参见使用多种环境

For further details on configuration of the launchSettings.json file see Working with multiple environments

请注意,commandName可能需要为"Project"(我并没有做太多尝试).示例launchSettings.json如下:

Note that the commandName would probably need to be "Project" (I haven't really tried this much). Example launchSettings.json as follows:

{ "iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { "applicationUrl": "localhost:19882/", "sslPort": 0 } }, "profiles": { "Development": { "commandName": "Project", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } } } }

更多推荐

在.NET Core 2.0中设置环境变量

本文发布于:2023-11-08 11:02:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569195.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:环境变量   NET   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!