使用 Visual Studio 進行 ASP.NET Web 部署:部署額外檔案
演講者:Tom Dykstra
本教學課程系列介紹如何使用 Visual Studio 2012 或 Visual Studio 2010 將 ASP.NET Web 應用程式部署 (發佈) 至 Azure App Service Web Apps 或第三方託管提供者。 如需本系列的資訊,請參閱本系列的第一個教學課程。
本教學課程示範如何擴充 Visual Studio Web 發佈管線,以在部署期間執行其他任務。 任務是將專案資料夾中沒有的額外檔案複製到目標網站。
在本教學課程中,您將複製一個額外檔案:robots.txt。 您希望將此文件部署到預備階段,但不部署到生產環境。 在「部署到生產環境」教學課程中,您已將此檔案新增至專案並設定生產環境發佈設定檔以將其排除。 在本教學課程中,您將看到一種處理這種情況的替代方法,這種方法對於您想要部署但不想包含在專案中的任何檔案非常有用。
為了準備處理 robots.txt 的不同方法,在本教學課程的這一節中,您將把該檔案移動到不包含在專案中的資料夾,並從預備環境中刪除 robots.txt。 有必要從預備環境中刪除該文件,以便您可以驗證將文件部署到該環境的新方法是否正常運作。
在方案總管中,以滑鼠以滑鼠右鍵按一下 robots.txt 檔案,然後按一下「從專案中排除」。
使用 Windows 檔案總管,在解決方案資料夾中建立一個新資料夾並將其命名為 ExtraFiles。
將 robots.txt 檔案從 ContosoUniversity 專案資料夾移至 ExtraFiles 資料夾。
使用 FTP 工具,從預備網站刪除 robots.txt 檔案。
或者,您可以在預備發佈設定檔的「設定」標籤上的「檔案發佈選項」下選擇「移除目的地上的其他檔案」,然後重新發佈到預備環境。
您只需要在預備環境中使用 robots.txt,因此為了部署它,您唯一需要更新的發佈設定檔就是 Staging。
在 Visual Studio 中,開啟 Staging.pubxml。
在文件結尾的結束
</Project>
標籤之前,請新增以下標記:<Target Name="CustomCollectFiles"> <ItemGroup> <_CustomFiles Include="..\ExtraFiles\**\*" /> <FilesForPackagingFromProject Include="%(_CustomFiles.Identity)"> <DestinationRelativePath>%(RecursiveDir)%(Filename)%(Extension)</DestinationRelativePath> </FilesForPackagingFromProject> </ItemGroup> </Target>
此程式碼會建立一個新目標,該目標將收集要部署的其他檔案。 目標由 MSBuild 將根據您指定的條件執行的一項或多項任務組成。
Include
屬性指定要在其中尋找檔案的資料夾是 ExtraFiles,與專案資料夾位於相同層級。 MSBuild 將從該資料夾收集所有文件,並從任何子資料夾遞歸收集 (雙星號指定遞歸子資料夾)。 使用此程式碼,您可以將多個檔案以及 ExtraFiles 資料夾內的子資料夾中的檔案放入其中,並且所有檔案都將部署。DestinationRelativePath
元素指定資料夾和檔案應複製到目標網站的根資料夾,其檔案和資料夾結構與 ExtraFiles 資料夾中的檔案和資料夾結構相同。 如果您想要複製 ExtraFiles 資料夾本身,則DestinationRelativePath
值將為 ExtraFiles\%(RecursiveDir)%(Filename)%(Extension)。在檔案結尾的結束
</Project>
標籤之前,新增以下標記來指定何時執行新目標。<PropertyGroup> <CopyAllFilesToSingleFolderForPackageDependsOn> CustomCollectFiles; $(CopyAllFilesToSingleFolderForPackageDependsOn); </CopyAllFilesToSingleFolderForPackageDependsOn> <CopyAllFilesToSingleFolderForMsdeployDependsOn> CustomCollectFiles; $(CopyAllFilesToSingleFolderForMsdeployDependsOn); </CopyAllFilesToSingleFolderForMsdeployDependsOn> </PropertyGroup>
每當執行將檔案複製到目標資料夾的目標時,此程式碼都會執行新
CustomCollectFiles
目標。 發佈與部署包建立有一個單獨的目標,如果您決定使用部署包而不是發佈進行部署,則新目標將注入到兩個目標中。.pubxml 檔案現在類似於以下範例:
<?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. In order to learn more about this please visit https://go.microsoft.com/fwlink/?LinkID=208121. --> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <WebPublishMethod>MSDeploy</WebPublishMethod> <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration> <LastUsedPlatform>Any CPU</LastUsedPlatform> <SiteUrlToLaunchAfterPublish>http://contosou-staging.azurewebsites.net</SiteUrlToLaunchAfterPublish> <ExcludeApp_Data>True</ExcludeApp_Data> <MSDeployServiceURL>waws-prod-bay-001.publish.azurewebsites.windows.net:443</MSDeployServiceURL> <DeployIisAppPath>contosou-staging</DeployIisAppPath> <RemoteSitePhysicalPath /> <SkipExtraFilesOnServer>False</SkipExtraFilesOnServer> <MSDeployPublishMethod>WMSVC</MSDeployPublishMethod> <UserName>$contosou-staging</UserName> <_SavePWD>True</_SavePWD> <PublishDatabaseSettings> <Objects xmlns=""> <ObjectGroup Name="SchoolContext" Order="1" Enabled="True"> <Destination Path="Data Source=tcp:sk0264hvc9.database.windows.net,1433;Initial Catalog=ContosoUniversity-staging;User ID=CU-staging-admin@sk0264hvc9;Password=" Name="Data Source=tcp:sk0264hvc9.database.windows.net,1433;Initial Catalog=ContosoUniversity-staging;User Id=CU-staging-admin@sk0264hvc9;Password=" /> <Object Type="DbCodeFirst"> <Source Path="DBMigration" DbContext="ContosoUniversity.DAL.SchoolContext, ContosoUniversity.DAL" MigrationConfiguration="ContosoUniversity.DAL.Migrations.Configuration, ContosoUniversity.DAL" Origin="Configuration" /> </Object> </ObjectGroup> <ObjectGroup Name="DefaultConnection" Order="2" Enabled="False"> <Destination Path="Data Source=tcp:sk0264hvc9.database.windows.net,1433;Initial Catalog=ContosoUniversity-staging;User ID=CU-staging-admin@sk0264hvc9;Password=" Name="Data Source=tcp:sk0264hvc9.database.windows.net,1433;Initial Catalog=ContosoUniversity-staging;User Id=CU-staging-admin@sk0264hvc9;Password=" /> <Object Type="DbDacFx"> <PreSource Path="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-ContosoUniversity.mdf;Initial Catalog=aspnet-ContosoUniversity;Integrated Security=True" includeData="False" /> <Source Path="$(IntermediateOutputPath)AutoScripts\DefaultConnection_IncrementalSchemaOnly.dacpac" dacpacAction="Deploy" /> </Object> <UpdateFrom Type="Web.Config"> <Source MatchValue="Data Source=(LocalDb)\v11.0;Integrated Security=SSPI;Initial Catalog=aspnet-ContosoUniversity;AttachDBFilename=|DataDirectory|\aspnet-ContosoUniversity.mdf" MatchAttributes="$(UpdateFromConnectionStringAttributes)" /> </UpdateFrom> <Object Type="DbFullSql" Enabled="False"> <Source Path="..\aspnet-data-prod.sql" Transacted="False" /> </Object> </ObjectGroup> </Objects> </PublishDatabaseSettings> <EnableMSDeployBackup>False</EnableMSDeployBackup> </PropertyGroup> <ItemGroup> <MSDeployParameterValue Include="$(DeployParameterPrefix)DefaultConnection-Web.config Connection String"> <ParameterValue>Data Source=tcp:sk0264hvc9.database.windows.net,1433;Initial Catalog=ContosoUniversity-staging;User Id=CU-staging-admin@sk0264hvc9;Password=</ParameterValue> </MSDeployParameterValue> <MSDeployParameterValue Include="$(DeployParameterPrefix)SchoolContext-Web.config Connection String"> <ParameterValue>Data Source=tcp:sk0264hvc9.database.windows.net,1433;Initial Catalog=ContosoUniversity-staging;User Id=CU-staging-admin@sk0264hvc9;Password=</ParameterValue> </MSDeployParameterValue> </ItemGroup> <Target Name="CustomCollectFiles"> <ItemGroup> <_CustomFiles Include="..\ExtraFiles\**\*" /> <FilesForPackagingFromProject Include="%(_CustomFiles.Identity)"> <DestinationRelativePath>%(RecursiveDir)%(Filename)%(Extension)</DestinationRelativePath> </FilesForPackagingFromProject> </ItemGroup> </Target> <PropertyGroup> <CopyAllFilesToSingleFolderForPackageDependsOn> CustomCollectFiles; $(CopyAllFilesToSingleFolderForPackageDependsOn); </CopyAllFilesToSingleFolderForPackageDependsOn> <CopyAllFilesToSingleFolderForMsdeployDependsOn> CustomCollectFiles; $(CopyAllFilesToSingleFolderForMsdeployDependsOn); </CopyAllFilesToSingleFolderForMsdeployDependsOn> </PropertyGroup> </Project>
儲存並關閉 Staging.pubxml 檔案。
使用一鍵發佈或命令列,透過預備設定檔發佈應用程式。
如果您使用一鍵發佈,您可以在預覽視窗中驗證是否會複製 robots.txt。 否則,請在部署後使用 FTP 工具驗證 robots.txt 檔案是否位於網站的根資料夾中。
關於將 ASP.NET Web 應用程式部署到第三方託管提供者的系列教學課程到此結束。 有關這些教學課程中涵蓋的任何主題的詳細資訊,請參閱 ASP.NET 部署內容對應。
如果您知道如何使用 MSBuild 檔案,則可以透過在.pubxml 檔案 (對於特定於設定檔的任務) 或專案 .wpp.targets 檔案 (適用於所有設定檔的任務) 中編寫程式碼來自動執行許多其他部署任務。 有關 .pubxml 和 .wpp.targets 檔案的詳細資訊,請參閱「作法:編輯發佈設定檔 (.pubxml) 檔案中的部署設定和 Visual Studio Web 專案中的 .wpp.targets 檔案」。 有關 MSBuild 程式碼的基本介紹,請參閱「企業部署系列:以了解專案檔案」中的專案檔案剖析。 要了解如何使用 MSBuild 檔案來執行適合您自己的場景的任務,請參閱這本書:Inside the Microsoft Build Engine:Using MSBuild and Team Foundation Build,作者是 Sayed Ibraham Hashimi 和 William Bartholomew。
我要感謝以下為本教學課程系列的內容做出重大貢獻的人員:
- Alberto Poblacion,MVP & MCT,西班牙
- Jarod Ferguson,Data Platform Development MVP,美國
- Harsh Mittal,Microsoft
- Jon Galloway (twitter: @jongalloway)
- Kristina Olson,Microsoft
- Mike Pope,Microsoft
- Mohit Srivastava,Microsoft
- Raffaele Rialdi,義大利
- Rick Anderson,Microsoft
- Sayed Hashimi,Microsoft(twitter: @sayedihashimi)
- Scott Hanselman (twitter: @shanselman)
- Scott Hunter,Microsoft (twitter: @coolcsh)
- Srđan Božović,Serbia
- Vishal Joshi,Microsoft (twitter: @vishalrjoshi)