gpt4 book ai didi

visual-studio - IHostingEnvironment.EnvironmentName如何工作?

转载 作者:行者123 更新时间:2023-12-04 03:49:22 25 4
gpt4 key购买 nike

在.NET Core控制台应用程序中,如果我添加以下行...

IHostingEnvironment env = new HostingEnvironment();
Console.WriteLine(env.EnvironmentName);

我得到这个结果...

Production



但是,当我在同一台计算机上的ASP.NET Core应用程序中执行相同的操作时...
public Startup(IHostingEnvironment env)
{
Debug.WriteLine(env.EnvironmentName);
}

我得到这个结果...

Development


  • EnvironmentName到底如何工作?
  • 如何指定我的本地计算机是开发环境?
  • 如何指定Azure是生产环境?


  • 另外,可以将EnvironmentName配置为与解决方案中的 调试发布配置一起使用吗?

    enter image description here
  • 调试=>开发
  • 发布=>生产

  • 最终目标是当使用 调试构建时能够连接到本地SQL数据库,而当使用 发行构建时能够连接到Azure数据库。

    最佳答案

    How exactly does EnvironmentName work?



    .NET Core读取名称 from an environmental variable

    How can I specify that my local machine is a Development environment?



    将环境变量设置为 Development

    How can I specify that Azure is a Production environment?



    将环境变量设置为 Production

    As an extra point, can EnvironmentName be configured to work with Debug and Release configurations within the solution?



    您可以创建一个启动配置文件来设置环境名称,并将该配置文件与Debug或Release配置一起使用。从Visual Studio启动时,启动配置文件会影响 EnvironmentName。在其他环境中运行应用程序时,您将需要使用其他方式进行设置。

    下图显示了在Release配置中运行应用程序以及将 EnvironmentName设置为 Development的MyDevProfile。

    enter image description here

    关于visual-studio - IHostingEnvironment.EnvironmentName如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40515939/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com