iis 如何从dotnet发布配置dotnet核心web.config?

vtwuwzda  于 2022-11-12  发布在  其他
关注(0)|答案(2)|浏览(177)

我的dotnet core 3.1 webapp tasks.json中有以下内容:

"env": {
            "ASPNETCORE_ENVIRONMENT": "Development",
            "ASPNETCORE_URLS": "http://localhost:5000",
            "LOCAL": "true"
        },

当我运行dotnet publish时,Debug web.config包含以下内容:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="dotnet" arguments=".\App.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="OutOfProcess" />
    </system.webServer>
  </location>
</configuration>

因为我想在IIS下托管,这就是我希望它包含的内容,所以现在我需要手动编辑它:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="dotnet" arguments=".\App.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="OutOfProcess">
        <environmentVariables>
          <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
          <environmentVariable name="ASPNETCORE_URLS" value="http://localhost/app" />
          <environmentVariable name="LOCAL" value="true" />
        </environmentVariables>
      </aspNetCore>
    </system.webServer>
  </location>
</configuration>

当我运行dotnet publish时,如何配置我的解决方案或.vscode .json文件以进行此更改?

ibps3vxo

ibps3vxo1#

1)您可以使用以下命令在发布时添加环境变量:

dotnet publish -c Debug -r /p:EnvironmentName=Development

2)在发布配置文件中添加环境变量

<PropertyGroup>
  <EnvironmentName>Development</EnvironmentName>
</PropertyGroup>

3)修改项目文件(.csProj)文件
引用链接:
Publish to IIS, setting Environment Variable
how to set ASPNETCORE_ENVIRONMENT to be considered for publishing an asp.net core application?

lf5gs5x2

lf5gs5x22#

如果要执行的操作不只是设置EnvironmentName
在使用"Microsoft.NET.Sdk.Web"目标的项目上,将应用配置转换。
在项目文件夹中添加一个web.{CONFIGURATION}.config

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <location>
    <system.webServer>
      <aspNetCore>
        <environmentVariables xdt:Transform="InsertIfMissing">
          <environmentVariable name="Configuration_Specific" 
                               value="Configuration_Specific_Value" 
                               xdt:Locator="Match(name)" 
                               xdt:Transform="InsertIfMissing" />
        </environmentVariables>
      </aspNetCore>
    </system.webServer>
  </location>
</configuration>

然后,在运行dotnet publish -c {CONFIGURATION}之后,发布输出包含以下自动生成的web.config(为简洁起见,删除了一些部分):

...
<aspNetCore ...>
  <environmentVariables>
    <environmentVariable name="Configuration_Specific" value="Configuration_Specific_Value"/>
  </environmentVariables>
</aspNetCore>
...

请访问https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/transform-webconfig?view=aspnetcore-6.0#build-configuration

相关问题