英文:
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
环境变量,而不必更改和重新部署应用程序。
- 打开IIS管理器。
- 选择要设置托管环境的网站。
- 单击“Configuration Editor”功能。
- 在“Configuration Editor”中,在下拉菜单中选择
system.webServer/aspNetCore
,然后在右侧选择ApplicationHost.config
。如果你忘记选择ApplicationHost.config
,则会在运行站点的使用的web.config
中设置此变量。然后,在部署新版本的网站时,它将被覆盖。ApplicationHost.config
设置为机器级别,因此该设置将位于不同的位置。 - 标记
environmentVariables
行,然后单击末尾的三个点以编辑列表。 - 单击“添加”按钮,并将
name
设置为ASPNETCORE_ENVIRONMENT
,将value
设置为Staging
。 - 重新启动网站以使更改生效。
你还可以通过代码或配置使用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.
- Open IIS Manager
- Select the website for which you want to set the hosting environment
- Click on the "Configuration Editor" feature
- In the the "Configuration Editor" select
system.webServer/aspNetCore
in the dropdown and just to the right og this, REMEMBER to selectApplicationHost.config
. If you fotget, you will set this variable in the usedweb.config
for the running site. Then ofcause it will be overwritten when you deploy a new version of the website. TheApplicationHost.config
sets it machine level, so the setting will live in a deffenrent place. - Mark environmentVariables line and click the tree dots at the end to edit the list.
- Click the Add button and set the
name
toASPNETCORE_ENVIRONMENT
andvalue
toStaging
- 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");
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论