![](/img/trans.png)
[英]What is the equivalent of Web.config transform in ASP.NET Core?
[英]Set web.config transform in Asp.NET Core
我剛剛遇到了 asp.net 核心中 web.config 轉換的問題。
有兩個文件:base web.config 和 web.prod-zone-a.config。 我的目標是在發布我的項目時在 web.prod-zone-a.config 中使用轉換。 我在 .csproj 中有以下“prod-zone-a”配置設置:
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'prod-zone-a|AnyCPU' ">
<IntermediateOutputPath>obj\Debug\netcoreapp1.1</IntermediateOutputPath>
<DebugSymbols>true</DebugSymbols>
<Optimize>false</Optimize>
<DefineConstants>TRACE;DEBUG;NETCOREAPP1_1</DefineConstants>
<Configuration>prod-zone-a</Configuration>
</PropertyGroup>
web.prod-zone-a.config 看起來像:
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<aspNetCore>
<environmentVariables xdt:Transform="Replace">
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="prod-zone-a" />
</environmentVariables>
</aspNetCore>
</system.webServer>
我嘗試通過兩個命令運行發布:
dotnet msbuild /t:Publish /p:OutputPath=c:\delivery /p:Configuration=prod-zone-a
和
dotnet publish --configuration prod-zone-a --output c:\delivery
但是沒有轉換適用於輸出的 web.config - 只是默認值。 我是否錯過了配置或命令執行中的某些內容?
這對我有用:
web.release.config
文件添加到項目根目錄。樣本轉換:
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<system.webServer>
<aspNetCore>
<environmentVariables>
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="PRODUCTION" xdt:Locator="Match(name)" xdt:Transform="SetAttributes" />
</environmentVariables>
</aspNetCore>
</system.webServer>
</configuration>
更新:如果您想在發布時刪除web.config.release
文件和其他文件,只需編輯您的.csproj文件並添加如下內容:
<ItemGroup>
<Content Remove="appsettings.Development.json" />
<Content Remove="web.release.config" />
</ItemGroup>
<ItemGroup>
<None Include="appsettings.Development.json" />
<None Include="web.release.config" />
</ItemGroup>
使用最新版本的 dotnet cli(2.1.400 或更高版本),您只需設置此 msbuild 屬性 $(EnvironmentName) 並且發布工具將負責將 ASPNETCORE_ENVIRONMENT environmentVariable 添加到具有指定環境名稱的 web.config。
此外,從 2.2.100-preview1 開始提供 XDT 支持。
示例: https : //github.com/vijayrkn/webconfigtransform/blob/master/README.md
繼上面 user1820686 的回答之后:
github 頁面遺漏了為 MSBuild/csproj 工具添加它所需的一些步驟:
您需要在項目目錄中打開命令提示符並運行
dotnet add myProj.csproj package Microsoft.DotNet.Xdt.Tools --version 2.0.0
然后你需要打開csproj文件並添加
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
<DotNetCliToolReference Include="Microsoft.Dotnet.Xdt.Tools" Version="2.0.0" />
<!-- ... other package references ... -->
</ItemGroup>
可能是我不清楚問題。 對於我的案例 web.config 覆蓋 web.Release.config 文件中的所有設置。
為我修復,我只是將轉換xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform" to configuration
參考添加xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform" to configuration
文件。
所以,.config 文件應該開始:
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
一段時間后,最好的解決方案是使用dotnet-transform-xdt 工具
在 Visual Studio 2017 (VS2017) 中的 IIS Web 部署 ASP.NET Core (2.1)
首先這樣做:(參考: https : //github.com/nil4/dotnet-transform-xdt#-use-with-msbuildcsproj-tooling )
dotnet add package DotNet.Xdt --version 2.1.0
ApplyXdtConfigTransform
) - 參考githubDEV_Server.pubxml
添加web.DEV_Server.config
轉換文件web.DEV_Server.config
添加了以下web.DEV_Server.config
<environmentVariable xdt:Locator="Match(name)" name="ASPNETCORE_ENVIRONMENT" value="Development" xdt:Transform="SetAttributes" />
DEV_Server.pubxml
以修改以下設置值。 <LastUsedBuildConfiguration>DEV_Server</LastUsedBuildConfiguration>
部署仍然上傳其他配置文件,不知道如何停止。
SDK 版本 2.2 的dotnet publish
現在支持這一點,並提供大量選項。
我認為在問題的示例中,它會在發布為
dotnet publish --configuration prod-zone-a
這對我和上面的 1. & 2. 有用:
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<location>
<system.webServer>
<httpErrors existingResponse="PassThrough"
xdt:Locator="Match(existingResponse)"
xdt:Transform="InsertIfMissing" />
</system.webServer>
</location>
</configuration>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.