设置特定网站的托管环境如何?

huangapple go评论61阅读模式
英文:

How to set hosting environment for a specific website?

问题

我的网站在IIS中运行,与其他十几个网站一起。我想将其托管环境设置为Staging,但我无法使用ASPNETCORE_ENVIRONMENT环境变量进行设置,因为前述的其他网站。

我该如何为特定网站设置托管环境。

附注:找到了文档,其中详细介绍了这种特定情况。简而言之,修改web.config文件(是的,这个传统的文件)。

英文:

My site is running under IIS among a dozen others. I want to set its hosting environment to Staging, but I can't use ASPNETCORE_ENVIRONMENT environment variable to set it, because of the aforementioned other sites.

How can I set the hosting environment for a specific site.

P.S. Found the documentation that addresses this specific scenario. The short story is change web.config (yes, the legacy file).

答案1

得分: 3

<aspNetCore processPath="dotnet"
      arguments=".\MyApp.dll"
      stdoutLogEnabled="false"
      stdoutLogFile=".\logs\stdout"
      hostingModel="inprocess">
  <environmentVariables>
    <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
    <environmentVariable name="CONFIG_DIR" value="f:\application_config" />
  </environmentVariables>
</aspNetCore>

It is the same `web.config` generated by `dotnet publish`, so I believe you feel natural that further changes go to the same place.

Another way you might want is [to configure such environment variables on the application pool](https://learn.microsoft.com/en-us/iis/configuration/system.applicationhost/applicationpools/add/environmentvariables/add).
英文:

While it is not easy to find, but when Microsoft documented ASP.NET Core module they did provide sample code on how to set environment variables in web.config, so there is no need to follow the other answer to modify applicationHost.config.

<aspNetCore processPath="dotnet"
      arguments=".\MyApp.dll"
      stdoutLogEnabled="false"
      stdoutLogFile=".\logs\stdout"
      hostingModel="inprocess">
  <environmentVariables>
    <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
    <environmentVariable name="CONFIG_DIR" value="f:\application_config" />
  </environmentVariables>
</aspNetCore>

It is the same web.config generated by dotnet publish, so I believe you feel natural that further changes go to the same place.

Another way you might want is to configure such environment variables on the application pool.

答案2

得分: 0

你可以在IIS内部覆盖ASPNETCORE_ENVIRONMENT环境变量,而不必更改和重新部署应用程序。

  1. 打开IIS管理器。
  2. 选择要设置托管环境的网站。
  3. 单击“Configuration Editor”功能。
  4. 在“Configuration Editor”中,在下拉菜单中选择system.webServer/aspNetCore,然后在右侧选择ApplicationHost.config。如果你忘记选择ApplicationHost.config,则会在运行站点的使用的web.config中设置此变量。然后,在部署新版本的网站时,它将被覆盖。ApplicationHost.config设置为机器级别,因此该设置将位于不同的位置。
  5. 标记environmentVariables行,然后单击末尾的三个点以编辑列表。
  6. 单击“添加”按钮,并将name设置为ASPNETCORE_ENVIRONMENT,将value设置为Staging
  7. 重新启动网站以使更改生效。

你还可以通过代码或配置使用UseEnvironment方法来设置环境。请注意,你需要重新部署应用程序才能应用这些更改。

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseEnvironment("Staging");
}
英文:

You can override ASPNETCORE_ENVIRONMENT for the single site inside IIS without changing and redeploying your app.

  1. Open IIS Manager
  2. Select the website for which you want to set the hosting environment
  3. Click on the "Configuration Editor" feature 设置特定网站的托管环境如何?
  4. In the the "Configuration Editor" select system.webServer/aspNetCore in the dropdown and just to the right og this, REMEMBER to select ApplicationHost.config. If you fotget, you will set this variable in the used web.config for the running site. Then ofcause it will be overwritten when you deploy a new version of the website. The ApplicationHost.config sets it machine level, so the setting will live in a deffenrent place.
  5. Mark environmentVariables line and click the tree dots at the end to edit the list.设置特定网站的托管环境如何?
  6. Click the Add button and set the name to ASPNETCORE_ENVIRONMENT and value to Staging 设置特定网站的托管环境如何?
  7. Restart the website for the changes to take effect.

Also you can set environment from code or configuration via UseEnvironment method. Note that you need to redeploy an application to apply this changes:

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseEnvironment("Staging");
}

huangapple
  • 本文由 发表于 2023年2月14日 05:22:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75441303.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定