適用於 ASP.NET Core 應用程式部署的 Visual Studio 發行設定檔Visual Studio publish profiles for ASP.NET Core app deployment

作者:Sayed Ibrahim HashimiRick AndersonBy Sayed Ibrahim Hashimi and Rick Anderson

本文將焦點放在使用 Visual Studio 2017 或更新版本來建立及使用發行設定檔。This document focuses on using Visual Studio 2017 or later to create and use publish profiles. 使用 Visual Studio 建立的發行設定檔,可從 MSBuild 和 Visual Studio 執行。The publish profiles created with Visual Studio can be run from MSBuild and Visual Studio. 如需發行到 Azure 的指示,請參閱使用 Visual Studio 將 ASP.NET Core Web 應用程式發行到 Azure App ServiceSee Publish an ASP.NET Core web app to Azure App Service using Visual Studio for instructions on publishing to Azure.

以下是使用 dotnet new mvc命令來建立的專案檔:The following project file was created with the command dotnet new mvc:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.2</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" />
  </ItemGroup>

</Project>
<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" />
  </ItemGroup>

</Project>

<Project> 元素的 Sdk 屬性會完成下列工作:The <Project> element's Sdk attribute accomplishes the following tasks:

  • 一開始會從 $(MSBuildSDKsPath)\Microsoft.NET.Sdk.Web\Sdk\Sdk.Props 匯入屬性檔案。Imports the properties file from $(MSBuildSDKsPath)\Microsoft.NET.Sdk.Web\Sdk\Sdk.Props at the beginning.
  • 從結尾的 $(MSBuildSDKsPath)\Microsoft.NET.Sdk.Web\Sdk\Sdk.targets 匯入目標檔案。Imports the targets file from $(MSBuildSDKsPath)\Microsoft.NET.Sdk.Web\Sdk\Sdk.targets at the end.

MSBuildSDKsPath (與 Visual Studio 2017 Enterprise) 的預設位置在 %programfiles(x86)%\Microsoft Visual Studio\2017\Enterprise\MSBuild\Sdks 資料夾。The default location for MSBuildSDKsPath (with Visual Studio 2017 Enterprise) is the %programfiles(x86)%\Microsoft Visual Studio\2017\Enterprise\MSBuild\Sdks folder.

Microsoft.NET.Sdk.Web SDK 依存於:The Microsoft.NET.Sdk.Web SDK depends on:

  • Microsoft.NET.Sdk.Web.ProjectSystemMicrosoft.NET.Sdk.Web.ProjectSystem
  • Microsoft.NET.Sdk.PublishMicrosoft.NET.Sdk.Publish

這會導致匯入下列屬性和目標:Which causes the following properties and targets to be imported:

  • $(MSBuildSDKsPath)\Microsoft.NET.Sdk.Web.ProjectSystem\Sdk\Sdk.Props$(MSBuildSDKsPath)\Microsoft.NET.Sdk.Web.ProjectSystem\Sdk\Sdk.Props
  • $(MSBuildSDKsPath)\Microsoft.NET.Sdk.Web.ProjectSystem\Sdk\Sdk.targets$(MSBuildSDKsPath)\Microsoft.NET.Sdk.Web.ProjectSystem\Sdk\Sdk.targets
  • $(MSBuildSDKsPath)\Microsoft.NET.Sdk.Publish\Sdk\Sdk.Props$(MSBuildSDKsPath)\Microsoft.NET.Sdk.Publish\Sdk\Sdk.Props
  • $(MSBuildSDKsPath)\Microsoft.NET.Sdk.Publish\Sdk\Sdk.targets$(MSBuildSDKsPath)\Microsoft.NET.Sdk.Publish\Sdk\Sdk.targets

發佈目標會根據所使用的發佈方法,匯入一組正確的目標。Publish targets import the right set of targets based on the publish method used.

當 MSBuild 或 Visual Studio 載入專案時,會執行下列高階動作:When MSBuild or Visual Studio loads a project, the following high-level actions occur:

  • 建置專案Build project
  • 計算要發佈的檔案Compute files to publish
  • 將檔案發佈至目的地Publish files to destination

計算專案項目Compute project items

載入專案時,會計算專案項目 (檔案)。When the project is loaded, the project items (files) are computed. item type 屬性會決定如何處理檔案。The item type attribute determines how the file is processed. 根據預設,.cs 檔案會包含在 Compile 項目清單中。By default, .cs files are included in the Compile item list. Compile 項目清單中的檔案會予以編譯。Files in the Compile item list are compiled.

Content 項目清單包含除了組建輸出之外還會另行發佈的檔案。The Content item list contains files that are published in addition to the build outputs. 符合 wwwroot/** 模式的檔案預設會包含在 Content 項目中。By default, files matching the pattern wwwroot/** are included in the Content item. wwwroot/\*\* 萬用字元模式會比對 [wwwroot] 資料夾子資料夾中的所有檔案。The wwwroot/\*\* globbing pattern matches all files in the wwwroot folder and subfolders. 若要明確地將檔案新增至發佈清單,請直接在 .csproj 檔案中新增檔案,如包含檔案中所示。To explicitly add a file to the publish list, add the file directly in the .csproj file as shown in Include Files.

在 Visual Studio 中選取 [發佈] 按鈕,或從命令列發佈時:When selecting the Publish button in Visual Studio or when publishing from the command line:

  • 會計算屬性/項目 (需要建置的檔案)。The properties/items are computed (the files that are needed to build).
  • 僅限 Visual Studio:會還原 NuGet 套件。Visual Studio only: NuGet packages are restored. (CLI 的使用者要明確還原。)(Restore needs to be explicit by the user on the CLI.)
  • 專案隨即建置。The project builds.
  • 會計算的發佈項目 (需要發佈的檔案)。The publish items are computed (the files that are needed to publish).
  • 會發佈專案 (計算的檔案會複製到發佈目的地)。The project is published (the computed files are copied to the publish destination).

當 ASP.NET Core 專案參考專案檔中的 Microsoft.NET.Sdk.Web 時,app_offline.htm 檔案會放在 Web 應用程式目錄的根目錄中。When an ASP.NET Core project references Microsoft.NET.Sdk.Web in the project file, an app_offline.htm file is placed at the root of the web app directory. 當檔案存在時,ASP.NET Core 模組會正常關閉應用程式,並在部署期間提供 app_offline.htm 檔案。When the file is present, the ASP.NET Core Module gracefully shuts down the app and serves the app_offline.htm file during the deployment. 如需詳細資訊,請參閱 ASP.NET Core 模組組態參考For more information, see the ASP.NET Core Module configuration reference.

基本命令列發佈Basic command-line publishing

命令列發佈適用於所有 .NET Core 支援的平台,而不需要 Visual Studio。Command-line publishing works on all .NET Core-supported platforms and doesn't require Visual Studio. 在下列範例中,是從專案目錄 (其中包含 .csproj 檔案) 執行 dotnet publish 命令。In the samples below, the dotnet publish command is run from the project directory (which contains the .csproj file). 如果不在專案資料夾中,請以明確方式傳入專案檔路徑。If not in the project folder, explicitly pass in the project file path. 例如:For example:

dotnet publish C:\Webs\Web1

執行下列命令來建立及發佈 Web 應用程式:Run the following commands to create and publish a web app:

dotnet new mvc
dotnet publish

dotnet publish 命令會產生類似以下的輸出:The dotnet publish command produces output similar to the following:

C:\Webs\Web1>dotnet publish
Microsoft (R) Build Engine version 15.3.409.57025 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  Web1 -> C:\Webs\Web1\bin\Debug\netcoreapp{X.Y}\Web1.dll
  Web1 -> C:\Webs\Web1\bin\Debug\netcoreapp{X.Y}\publish\

預設發佈資料夾是 bin\$(Configuration)\netcoreapp<version>\publishThe default publish folder is bin\$(Configuration)\netcoreapp<version>\publish. $(Configuration) 的預設值是 DebugThe default for $(Configuration) is Debug. 在上述範例中,<TargetFramework>netcoreapp{X.Y}In the preceding sample, the <TargetFramework> is netcoreapp{X.Y}.

dotnet publish -h 顯示發佈的說明資訊。dotnet publish -h displays help information for publish.

下列命令會指定 Release 組建和發佈目錄:The following command specifies a Release build and the publishing directory:

dotnet publish -c Release -o C:\MyWebs\test

dotnet publish 命令會呼叫 MSBuild 來叫用 Publish 目標。The dotnet publish command calls MSBuild, which invokes the Publish target. 所有傳遞給 dotnet publish 的參數都會傳遞給 MSBuild。Any parameters passed to dotnet publish are passed to MSBuild. -c 參數對應至 Configuration MSBuild 屬性。The -c parameter maps to the Configuration MSBuild property. -o 參數對應至 OutputPathThe -o parameter maps to OutputPath.

您可以使用下列兩種格式其中之一來傳遞 MSBuild 屬性:MSBuild properties can be passed using either of the following formats:

  • p:<NAME>=<VALUE>
  • /p:<NAME>=<VALUE>

下列命令會將 Release 組建發佈到網路共用:The following command publishes a Release build to a network share:

dotnet publish -c Release /p:PublishDir=//r8/release/AdminWeb

網路共用以正斜線 (//r8/) 指定,適用於所有 .NET Core 支援的平台。The network share is specified with forward slashes (//r8/) and works on all .NET Core supported platforms.

確認部署的已發佈應用程式未在執行中。Confirm that the published app for deployment isn't running. 當應用程式執行時,會鎖定 publish 資料夾中的檔案。Files in the publish folder are locked when the app is running. 因為無法複製鎖定的檔案,所以無法執行部署。Deployment can't occur because locked files can't be copied.

發行設定檔Publish profiles

本節會使用 Visual Studio 2017 或更新版本來建立發行設定檔。This section uses Visual Studio 2017 or later to create a publishing profile. 建立設定檔之後,即可從 Visual Studio 或命令列進行發佈。Once the profile is created, publishing from Visual Studio or the command line is available.

發行設定檔可以簡化發佈程序,且設定檔數目並無限制。Publish profiles can simplify the publishing process, and any number of profiles can exist. 請在 Visual Studio 中選擇下列其中一個路徑來建立設定檔:Create a publish profile in Visual Studio by choosing one of the following paths:

  • 在 [方案總管] 中的專案上按一下滑鼠右鍵,然後選取 [發佈]。Right-click the project in Solution Explorer and select Publish.
  • 從 [建置] 功能表選取 [發佈 {專案名稱}]。Select Publish {PROJECT NAME} from the Build menu.

應用程式容量頁面的 [發佈] 索引標籤隨即顯示。The Publish tab of the app capacities page is displayed. 如果專案缺少發行設定檔,則會顯示下列頁面:If the project lacks a publish profile, the following page is displayed:

應用程式容量頁面的 [發佈] 索引標籤

選取 [資料夾] 時,請指定要用來儲存所發佈資產的資料夾路徑。When Folder is selected, specify a folder path to store the published assets. 預設的資料夾是 bin\Release\PublishOutputThe default folder is bin\Release\PublishOutput. 請按一下 [建立設定檔] 按鈕來完成操作。Click the Create Profile button to finish.

建立發行設定檔之後,[發佈] 索引標籤就會變更。Once a publish profile is created, the Publish tab changes. 新建立的設定檔會出現在下拉式清單中。The newly created profile appears in a drop-down list. 按一下 [建立新設定檔] 即可建立另一個新的設定檔。Click Create new profile to create another new profile.

顯示 FolderProfile 的應用程式容量頁面 [發佈] 索引標籤

發佈精靈支援下列發佈目標:The Publish wizard supports the following publish targets:

  • Azure App ServiceAzure App Service
  • Azure 虛擬機器Azure Virtual Machines
  • IIS、FTP 等等 (適用於任何網頁伺服器)IIS, FTP, etc. (for any web server)
  • 資料夾Folder
  • 匯入設定檔Import Profile

如需詳細資訊,請參閱哪些發佈選項適合我?For more information, see What publishing options are right for me.

使用 Visual Studio 來建立發行設定檔時,會建立 Properties/PublishProfiles/{設定檔名稱}.pubxml MSBuild 檔案。When creating a publish profile with Visual Studio, a Properties/PublishProfiles/{PROFILE NAME}.pubxml MSBuild file is created. .pubxml 檔案是一個 MSBuild 檔案,而且包含發佈組態設定。The .pubxml file is a MSBuild file and contains publish configuration settings. 您可以變更這個檔案以自訂建置和發佈程序。This file can be changed to customize the build and publish process. 發佈程序會讀取這個檔案。This file is read by the publishing process. <LastUsedBuildConfiguration> 很特殊,因為它是全域屬性,不應該在組建中所匯入的任何檔案中。<LastUsedBuildConfiguration> is special because it's a global property and shouldn't be in any file that's imported in the build. 如需詳細資訊,請參閱 MSBuild:如何設定組態屬性 (英文)。See MSBuild: how to set the configuration property for more information.

發佈到 Azure 目標時,.pubxml 檔案會包含您的 Azure 訂用帳戶識別碼。When publishing to an Azure target, the .pubxml file contains your Azure subscription identifier. 使用該目標類型時,不建議將此檔案新增至原始檔控制。With that target type, adding this file to source control is discouraged. 發佈到非 Azure 目標時,可放心簽入 .pubxml 檔案。When publishing to a non-Azure target, it's safe to check in the .pubxml file.

機密資訊 (例如發佈密碼) 會依每個使用者/電腦加密。Sensitive information (like the publish password) is encrypted on a per user/machine level. 其儲存位置是在 Properties/PublishProfiles/{設定檔名稱}.pubxml.user 檔案中。It's stored in the Properties/PublishProfiles/{PROFILE NAME}.pubxml.user file. 由於此檔案可以儲存機密資訊,因此不應該將它簽入至原始檔控制。Because this file can store sensitive information, it shouldn't be checked into source control.

如需如何在 ASP.NET Core 上發佈 Web 應用程式的概觀,請參閱裝載及部署For an overview of how to publish a web app on ASP.NET Core, see Host and deploy. 發佈 ASP.NET Core 應用程式所需的 MSBuild 工作和目標是可從 https://github.com/aspnet/websdk 取得的開放原始碼。The MSBuild tasks and targets necessary to publish an ASP.NET Core app are open-source at https://github.com/aspnet/websdk.

dotnet publish 可以使用資料夾、MSDeploy 及 Kudu 發行設定檔:dotnet publish can use folder, MSDeploy, and Kudu publish profiles:

資料夾 (可跨平台運作):Folder (works cross-platform):

dotnet publish WebApplication.csproj /p:PublishProfile=<FolderProfileName>

MSDeploy (目前僅適用於 Windows,因為 MSDeploy 無法跨平台運作):MSDeploy (currently this only works in Windows since MSDeploy isn't cross-platform):

dotnet publish WebApplication.csproj /p:PublishProfile=<MsDeployProfileName> /p:Password=<DeploymentPassword>

MSDeploy 套件 (目前僅適用於 Windows,因為 MSDeploy 無法跨平台運作):MSDeploy package (currently this only works in Windows since MSDeploy isn't cross-platform):

dotnet publish WebApplication.csproj /p:PublishProfile=<MsDeployPackageProfileName>

在先前的範例中,請「不要」將 deployonbuild 傳遞給 dotnet publishIn the preceding samples, don't pass deployonbuild to dotnet publish.

如需詳細資訊,請參閱 Microsoft.NET.Sdk.Publish(英文)。For more information, see Microsoft.NET.Sdk.Publish.

dotnet publish 支援使用 Kudu API 從任何平台發佈到 Azure。dotnet publish supports Kudu APIs to publish to Azure from any platform. Visual Studio 發佈支援 Kudu API,但 WebSDK 也支援使用它來跨平台發佈到 Azure。Visual Studio publish supports the Kudu APIs, but it's supported by WebSDK for cross-platform publish to Azure.

將含有下列內容的發行設定檔新增至 Properties/PublishProfiles 資料夾:Add a publish profile to the Properties/PublishProfiles folder with the following content:

<Project>
  <PropertyGroup>
    <PublishProtocol>Kudu</PublishProtocol>
    <PublishSiteName>nodewebapp</PublishSiteName>
    <UserName>username</UserName>
    <Password>password</Password>
  </PropertyGroup>
</Project>

執行下列命令以壓縮發佈內容,然後使用 Kudu API 將它發佈到 Azure:Run the following command to zip up the publish contents and publish it to Azure using the Kudu APIs:

dotnet publish /p:PublishProfile=Azure /p:Configuration=Release

使用發行設定檔時,請設定下列 MSBuild 屬性:Set the following MSBuild properties when using a publish profile:

  • DeployOnBuild=true
  • PublishProfile={PUBLISH PROFILE}

使用名為 FolderProfile 的設定檔來進行發佈時,可以執行下列兩個命令其中之一:When publishing with a profile named FolderProfile, either of the commands below can be executed:

  • dotnet build /p:DeployOnBuild=true /p:PublishProfile=FolderProfile
  • msbuild /p:DeployOnBuild=true /p:PublishProfile=FolderProfile

叫用 dotnet build 時,它會呼叫 msbuild 來執行建置和發佈程序。When invoking dotnet build, it calls msbuild to run the build and publish process. 傳入資料夾設定檔時,呼叫 dotnet buildmsbuild 是一樣的。Calling either dotnet build or msbuild is equivalent when passing in a folder profile. 直接在 Windows 上呼叫 MSBuild 時,會使用 .NET Framework 版本的 MSBuild。When calling MSBuild directly on Windows, the .NET Framework version of MSBuild is used. MSDeploy 目前僅限於 Windows 電腦進行發佈。MSDeploy is currently limited to Windows machines for publishing. 在非資料夾設定檔上呼叫 dotnet build 會叫用 MSBuild,而 MSBuild 會在非資料夾設定檔上使用 MSDeploy。Calling dotnet build on a non-folder profile invokes MSBuild, and MSBuild uses MSDeploy on non-folder profiles. 在非資料夾設定檔上呼叫 dotnet build,會叫用 MSBuild (使用 MSDeploy) 並造成失敗 (即使是在 Windows 平台上執行)。Calling dotnet build on a non-folder profile invokes MSBuild (using MSDeploy) and results in a failure (even when running on a Windows platform). 若要發佈非資料夾的設定檔,請直接呼叫 MSBuild。To publish with a non-folder profile, call MSBuild directly.

下列資料夾發行設定檔是以 Visual Studio 建立並發佈至網路共用:The following folder publish profile was created with Visual Studio and publishes to a network share:

<?xml version="1.0" encoding="utf-8"?>
<!--
This file is used by the publish/package process of your Web project.
You can customize the behavior of this process by editing this 
MSBuild file.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <WebPublishMethod>FileSystem</WebPublishMethod>
    <PublishProvider>FileSystem</PublishProvider>
    <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <SiteUrlToLaunchAfterPublish />
    <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
    <ExcludeApp_Data>False</ExcludeApp_Data>
    <PublishFramework>netcoreapp1.1</PublishFramework>
    <ProjectGuid>c30c453c-312e-40c4-aec9-394a145dee0b</ProjectGuid>
    <publishUrl>\\r8\Release\AdminWeb</publishUrl>
    <DeleteExistingFiles>False</DeleteExistingFiles>
  </PropertyGroup>
</Project>

請注意,<LastUsedBuildConfiguration> 設定為 ReleaseNote <LastUsedBuildConfiguration> is set to Release. 從 Visual Studio 發佈時,會使用發佈程序開始時的值來設定 <LastUsedBuildConfiguration> 組態屬性值。When publishing from Visual Studio, the <LastUsedBuildConfiguration> configuration property value is set using the value when the publish process is started. <LastUsedBuildConfiguration> 組態屬性很特殊,不應該在匯入的 MSBuild 檔案中被覆寫。The <LastUsedBuildConfiguration> configuration property is special and shouldn't be overridden in an imported MSBuild file. 您可以從命令列覆寫此屬性。This property can be overridden from the command line.

使用 .NET Core CLI:Using the .NET Core CLI:

dotnet build -c Release /p:DeployOnBuild=true /p:PublishProfile=FolderProfile

使用 MSBuild:Using MSBuild:

msbuild /p:Configuration=Release /p:DeployOnBuild=true /p:PublishProfile=FolderProfile

從命令列發佈至 MSDeploy 端點Publish to an MSDeploy endpoint from the command line

下列範例使用由 Visual Studio 建立的 ASP.NET Core Web 應用程式,名為 AzureWebAppThe following example uses an ASP.NET Core web app created by Visual Studio named AzureWebApp. 並使用 Visual Studio 新增了一個 Azure 應用程式發行設定檔。An Azure Apps publish profile is added with Visual Studio. 如需如何建立設定檔的詳細資訊,請參閱發行設定檔一節。For more information on how to create a profile, see the Publish profiles section.

若要使用發行設定檔部署應用程式,請從 Visual Studio 開發人員命令提示字元執行 msbuild 命令。To deploy the app using a publish profile, execute the msbuild command from a Visual Studio Developer Command Prompt. 您可以從 Windows 工作列 [開始] 功能表的 [Visual Studio] 資料夾中存取此命令提示字元。The command prompt is available in the Visual Studio folder of the Start menu on the Windows taskbar. 為方便存取,您可以將命令提示字元新增至 Visual Studio 的 [工具] 功能表。For easier access, you can add the command prompt to the Tools menu in Visual Studio. 如需詳細資訊,請參閱適用於 Visual Studio 的開發人員命令提示字元For more information, see Developer Command Prompt for Visual Studio.

MSBuild 使用下列命令語法:MSBuild uses the following command syntax:

msbuild {PATH} 
    /p:DeployOnBuild=true 
    /p:PublishProfile={PROFILE} 
    /p:Username={USERNAME} 
    /p:Password={PASSWORD}
  • {PATH} – 應用程式的專案檔路徑。{PATH} – Path to the app's project file.
  • {PROFILE} – 發行設定檔的名稱。{PROFILE} – Name of the publish profile.
  • {USERNAME} – MSDeploy 使用者名稱。{USERNAME} – MSDeploy username. {USERNAME} 可以在發行設定檔中找到。The {USERNAME} can be found in the publish profile.
  • {PASSWORD} – MSDeploy 密碼。{PASSWORD} – MSDeploy password. 您可以從 {PROFILE}.PublishSettings 檔案取得 {PASSWORD}。Obtain the {PASSWORD} from the {PROFILE}.PublishSettings file. 從下列其中一個位置下載 .PublishSettings 檔案:Download the .PublishSettings file from either:
    • 方案總管:選取 [檢視] > [Cloud Explorer]。Solution Explorer: Select View > Cloud Explorer. 連線到您的 Azure 訂用帳戶。Connect with your Azure subscription. 開啟 [應用程式服務]。Open App Services. 以滑鼠右鍵按一下應用程式。Right-click the app. 選取 [下載發行設定檔]。Select Download Publish Profile.
    • Azure 入口網站:在 Web 應用程式的 [概觀] 面板中,選取 [取得發行設定檔]。Azure portal: Select Get publish profile in the web app's Overview panel.

下列範例使用名為 AzureWebApp - Web Deploy 的發行設定檔:The following example uses a publish profile named AzureWebApp - Web Deploy:

msbuild "AzureWebApp.csproj" 
    /p:DeployOnBuild=true 
    /p:PublishProfile="AzureWebApp - Web Deploy" 
    /p:Username="$AzureWebApp" 
    /p:Password=".........."

您也可以從 Windows 命令提示字元透過 .NET Core CLI dotnet msbuild 命令來使用發行設定檔:A publish profile can also be used with the .NET Core CLI dotnet msbuild command from a Windows command prompt:

dotnet msbuild "AzureWebApp.csproj"
    /p:DeployOnBuild=true 
    /p:PublishProfile="AzureWebApp - Web Deploy" 
    /p:Username="$AzureWebApp" 
    /p:Password=".........."

注意

dotnet msbuild 命令可跨平台使用,並可在 macOS 和 Linux 上編譯 ASP.NET Core 應用程式。The dotnet msbuild command is available cross-platform and can compile ASP.NET Core apps on macOS and Linux. 不過,macOS 和 Linux 上的 MSBuild 無法將應用程式部署至 Azure 或其他 MSDeploy 端點。However, MSBuild on macOS and Linux isn't capable of deploying an app to Azure or other MSDeploy endpoint. 只有在 Windows 上才提供 MSDeploy。MSDeploy is only available on Windows.

設定環境Set the environment

在發行設定檔 (.pubxml) 或專案檔中包括 <EnvironmentName> 屬性,以設定應用程式的環境Include the <EnvironmentName> property in the publish profile (.pubxml) or project file to set the app's environment:

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

如果您需要進行 web.config 轉換 (例如根據設定、設定檔或環境設定環境變數),請參閱轉換 web.configIf you require web.config transformations (for example, setting environment variables based on the configuration, profile, or environment), see 轉換 web.config.

排除檔案Exclude files

發佈 ASP.NET Core Web 應用程式時,會包含 wwwroot 資料夾的組建成品和內容。When publishing ASP.NET Core web apps, the build artifacts and contents of the wwwroot folder are included. msbuild 支援通用慣例模式msbuild supports globbing patterns. 例如,下列 <Content> 元素會排除 wwwroot/content 資料夾及其所有子資料夾中的所有文字檔 (.txt)。For example, the following <Content> element excludes all text (.txt) files from the wwwroot/content folder and all its subfolders.

<ItemGroup>
  <Content Update="wwwroot/content/**/*.txt" CopyToPublishDirectory="Never" />
</ItemGroup>

您可以將上述標記新增至發行設定檔或 .csproj 檔案。The preceding markup can be added to a publish profile or the .csproj file. 當新增至 .csproj 檔案時,此規則會新增至專案中的所有發行設定檔。When added to the .csproj file, the rule is added to all publish profiles in the project.

下列 <MsDeploySkipRules> 元素會排除 wwwroot/content 資料夾中的所有檔案:The following <MsDeploySkipRules> element excludes all files from the wwwroot/content folder:

<ItemGroup>
  <MsDeploySkipRules Include="CustomSkipFolder">
    <ObjectName>dirPath</ObjectName>
    <AbsolutePath>wwwroot\\content</AbsolutePath>
  </MsDeploySkipRules>
</ItemGroup>

<MsDeploySkipRules> 不會從部署網站刪除「略過」目標。<MsDeploySkipRules> won't delete the skip targets from the deployment site. 這會從部署網站刪除 <Content> 鎖定目標的檔案和資料夾。<Content> targeted files and folders are deleted from the deployment site. 例如,假設所部署的 Web 應用程式包含下列檔案:For example, suppose a deployed web app had the following files:

  • Views/Home/About1.cshtmlViews/Home/About1.cshtml
  • Views/Home/About2.cshtmlViews/Home/About2.cshtml
  • Views/Home/About3.cshtmlViews/Home/About3.cshtml

如果新增下列 <MsDeploySkipRules> 元素,就不會刪除部署網站上的這些檔案。If the following <MsDeploySkipRules> elements are added, those files wouldn't be deleted on the deployment site.

<ItemGroup>
  <MsDeploySkipRules Include="CustomSkipFile">
    <ObjectName>filePath</ObjectName>
    <AbsolutePath>Views\\Home\\About1.cshtml</AbsolutePath>
  </MsDeploySkipRules>

  <MsDeploySkipRules Include="CustomSkipFile">
    <ObjectName>filePath</ObjectName>
    <AbsolutePath>Views\\Home\\About2.cshtml</AbsolutePath>
  </MsDeploySkipRules>

  <MsDeploySkipRules Include="CustomSkipFile">
    <ObjectName>filePath</ObjectName>
    <AbsolutePath>Views\\Home\\About3.cshtml</AbsolutePath>
  </MsDeploySkipRules>
</ItemGroup>

上述 <MsDeploySkipRules> 元素會防止部署「已略過」的檔案。The preceding <MsDeploySkipRules> elements prevent the skipped files from being deployed. 如果已部署這些檔案,它並不會將它們刪除。It won't delete those files once they're deployed.

下列 <Content> 元素會刪除部署網站上已鎖定目標的檔案:The following <Content> element deletes the targeted files at the deployment site:

<ItemGroup>
  <Content Update="Views/Home/About?.cshtml" CopyToPublishDirectory="Never" />
</ItemGroup>

使用命令列部署搭配上述 <Content> 元素會產生下列輸出:Using command-line deployment with the preceding <Content> element yields the following output:

MSDeployPublish:
  Starting Web deployment task from source: manifest(C:\Webs\Web1\obj\Release\netcoreapp1.1\PubTmp\Web1.SourceManifest.
  xml) to Destination: auto().
  Deleting file (Web11112\Views\Home\About1.cshtml).
  Deleting file (Web11112\Views\Home\About2.cshtml).
  Deleting file (Web11112\Views\Home\About3.cshtml).
  Updating file (Web11112\web.config).
  Updating file (Web11112\Web1.deps.json).
  Updating file (Web11112\Web1.dll).
  Updating file (Web11112\Web1.pdb).
  Updating file (Web11112\Web1.runtimeconfig.json).
  Successfully executed Web deployment task.
  Publish Succeeded.
Done Building Project "C:\Webs\Web1\Web1.csproj" (default targets).

Include 檔案Include files

下列標記會將專案目錄外的 [images] 資料夾包 含在發佈網站的 wwwroot/images 資料夾中:The following markup includes an images folder outside the project directory to the wwwroot/images folder of the publish site:

<ItemGroup>
  <_CustomFiles Include="$(MSBuildProjectDirectory)/../images/**/*" />
  <DotnetPublishFiles Include="@(_CustomFiles)">
    <DestinationRelativePath>wwwroot/images/%(RecursiveDir)%(Filename)%(Extension)</DestinationRelativePath>
  </DotnetPublishFiles>
</ItemGroup>

標記可以新增至 .csproj 檔案或發行設定檔。The markup can be added to the .csproj file or the publish profile. 如果將它新增至 .csproj 檔案,它就會包含在專案的每個發行設定檔中。If it's added to the .csproj file, it's included in each publish profile in the project.

下列反白顯示的標記會示範如何:The following highlighted markup shows how to:

  • 將專案外的檔案複製到 wwwroot 資料夾。Copy a file from outside the project into the wwwroot folder.
  • 排除 wwwroot\Content 資料夾。Exclude the wwwroot\Content folder.
  • 排除 Views\Home\About2.cshtmlExclude Views\Home\About2.cshtml.
<?xml version="1.0" encoding="utf-8"?>
<!--
This file is used by the publish/package process of your Web project.
You can customize the behavior of this process by editing this 
MSBuild file.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <WebPublishMethod>FileSystem</WebPublishMethod>
    <PublishProvider>FileSystem</PublishProvider>
    <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <SiteUrlToLaunchAfterPublish />
    <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
    <ExcludeApp_Data>False</ExcludeApp_Data>
    <PublishFramework />
    <ProjectGuid>afa9f185-7ce0-4935-9da1-ab676229d68a</ProjectGuid>
    <publishUrl>bin\Release\PublishOutput</publishUrl>
    <DeleteExistingFiles>False</DeleteExistingFiles>
  </PropertyGroup>
  <ItemGroup>
    <ResolvedFileToPublish Include="..\ReadMe2.MD">
      <RelativePath>wwwroot\ReadMe2.MD</RelativePath>
    </ResolvedFileToPublish>

    <Content Update="wwwroot\Content\**\*" CopyToPublishDirectory="Never" />
    <Content Update="Views\Home\About2.cshtml" CopyToPublishDirectory="Never" />

  </ItemGroup>
</Project>

如需更多的部署範例,請參閱 WebSDK 讀我檔案See the WebSDK Readme for more deployment samples.

在發佈之前或之後執行目標Run a target before or after publishing

內建的 BeforePublishAfterPublish 目標可在發佈目標之前或之後執行目標。The built-in BeforePublish and AfterPublish targets execute a target before or after the publish target. 將下列元素新增至發行設定檔,即可在發佈之前和之後都記錄主控台訊息:Add the following elements to the publish profile to log console messages both before and after publishing:

<Target Name="CustomActionsBeforePublish" BeforeTargets="BeforePublish">
    <Message Text="Inside BeforePublish" Importance="high" />
  </Target>
  <Target Name="CustomActionsAfterPublish" AfterTargets="AfterPublish">
    <Message Text="Inside AfterPublish" Importance="high" />
</Target>

使用未受信任的憑證來發佈至伺服器Publish to a server using an untrusted certificate

<AllowUntrustedCertificate> 屬性搭配 True 值新增至發行設定檔:Add the <AllowUntrustedCertificate> property with a value of True to the publish profile:

<PropertyGroup>
  <AllowUntrustedCertificate>True</AllowUntrustedCertificate>
</PropertyGroup>

Kudu 服務The Kudu service

若要檢視 Azure App Service Web 應用程式部署中的檔案,請使用 Kudu 服務To view the files in an Azure App Service web app deployment, use the Kudu service. scm 權杖附加至 Web 應用程式名稱。Append the scm token to the web app name. 例如:For example:

URLURL 結果Result
http://mysite.azurewebsites.net/ Web 應用程式Web App
http://mysite.scm.azurewebsites.net/ Kudu 服務Kudu service

選取偵錯主控台功能表項目,以檢視、編輯、刪除或新增檔案。Select the Debug Console menu item to view, edit, delete, or add files.

其他資源Additional resources