通过


使用 Visual Studio ASP.NET Web 部署:部署额外文件

作者:Tom Dykstra

下载入门项目

本教程系列介绍如何使用 Visual Studio 2012 或 Visual Studio 2010 将 ASP.NET Web 应用程序部署到 Azure 应用服务 Web 应用或第三方托管提供程序。 有关该系列的信息,请参阅 该系列中的第一个教程

概述

本教程介绍如何扩展 Visual Studio Web 发布管道,以在部署期间执行其他任务。 任务是将不在项目文件夹中的额外文件复制到目标网站。

在本教程中,你将复制一个额外的文件: robots.txt。 你想要将此文件部署到测试环境,而不部署到生产环境。 在“部署到生产”教程中,你已将此文件添加到项目,并配置了生产发布配置文件以排除该文件。 在本教程中,你将看到一种用于处理这种情况的替代方法,这对于要部署但不想包含在项目中的任何文件都很有用。

移动 robots.txt 文件

若要准备处理 robots.txt的不同方法,请在本教程的本节中将文件移动到项目中不包含的文件夹,并从暂存环境中删除 robots.txt 。 必须从暂存中删除文件,以便验证将文件部署到该环境的新方法是否正常工作。

  1. 解决方案资源管理器中,右键单击 robots.txt 文件,然后单击“ 从项目中排除”。

  2. 使用 Windows 文件资源管理器,在解决方案文件夹中创建新文件夹并将其命名 为 ExtraFiles

  3. robots.txt 文件从 ContosoUniversity 项目文件夹移动到 ExtraFiles 文件夹。

    ExtraFiles 文件夹

  4. 使用 FTP 工具从过渡网站中删除 robots.txt 文件。

    或者,您可以在暂存发布配置文件的设置选项卡下的文件发布选项中选择删除目标中的其他文件,然后重新发布到暂存环境。

更新发布配置文件

您只需要在预发布环境中包含robots.txt,因此,唯一需要更新的发布配置文件是用于预发布环境的配置文件,以便进行部署。

  1. 在 Visual Studio 中,打开 Staging.pubxml

  2. 在文件末尾,在结束 </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) 。

  3. 在文件末尾,在结束 </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>
    
  4. 保存并关闭 Staging.pubxml 文件。

发布到测试环境

使用一键发布或命令行,使用暂存配置文件发布应用程序。

如果使用一键式发布,则可以在 预览 窗口中验证是否将复制 robots.txt 。 否则,请使用 FTP 工具验证 部署后robots.txt 文件是否位于网站的根文件夹中。

总结

完成本系列教程,介绍如何将 ASP.NET Web 应用程序部署到第三方托管提供程序。 有关这些教程中介绍的任何主题的详细信息,请参阅 ASP.NET 部署内容映射

详细信息

如果知道如何使用 MSBuild 文件,可以通过在 .pubxml 文件(特定于配置文件的任务)或项目 .wpp.targets 文件(适用于应用于所有配置文件的任务)中编写代码来自动执行许多其他部署任务。 有关 .pubxml.wpp.targets 文件的详细信息,请参阅 如何:编辑 Visual Studio Web 项目中的发布配置文件 (.pubxml) 文件和 .wpp.targets 文件中的部署设置。 有关 MSBuild 代码的基本简介,请参阅企业部署系列中的项目文件的剖析:了解项目文件。 若要了解如何使用 MSBuild 文件为自己的方案执行任务,请参阅由 Sayed Ibraham Hashimi 和 William Bartholomew 编写的这本书:《深入微软生成引擎:使用 MSBuild 和 Team Foundation Build》

鸣谢

我要感谢以下为本教程系列内容做出重要贡献的人: