将额外文件添加到已发布的MVC API项目中

时间:2013-05-28 10:57:56

标签: c# asp.net-mvc msbuild publishing

我正在尝试向发布过程添加额外的XML文件。我有一个MVC API项目,它还有另一个控制器项目(V1.0)。我们使用自我文档帮助功能,为每个项目创建.XML文件。在本地计算机上构建时,一切正常,但在发布时(使用向导),它将不包含此文件。

我一直在尝试更新发布配置文件(.pubxml)文件,如下所述:

http://www.asp.net/mvc/tutorials/deployment/visual-studio-web-deployment/deploying-extra-files

但没有成功。我可以看到以下情况发生了:

  • 我干净利落,以确保无所事事。
  • 我使用向导发布
  • 我可以在apiproject \ bin \中找到所有文件,包括apiprojectv1 xml和dll文件
  • 我可以在apiproject \ obj \ x86 \ Release \ AspnetCompileMerge \ Source \ bin中看到它有apiprojectv1 dll而不是xml文件
  • 我在apiprojet \ obj \ x86 \ Release \ AspnetCompileMerge \ TempBuildDir \ bin
  • 中可以看到与上面相同的内容
  • 我在apiproject \ obj \ x86 \ Release \ Package \ PackageTmp \ bin
  • 中可以看到与上面相同的内容

我不确定为什么文件没有被复制。这是我的完整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     http://go.microsoft.com/fwlink/?LinkID=208121. 
-->
<Project ToolsVersion="4.0"     xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <WebPublishMethod>FileSystem</WebPublishMethod>
    <SiteUrlToLaunchAfterPublish />
    <publishUrl>\\myserver\wwwroot\apiproject</publishUrl>
    <DeleteExistingFiles>False</DeleteExistingFiles>
  </PropertyGroup>
  <Target Name="CustomCollectFiles">
        <ItemGroup>
          <_CustomFiles Include="..\bin\apiprojectv1.XML" />
          <FilesForPackagingFromProject Include="%(_CustomFiles.Identity)">
            <DestinationRelativePath>%(RecursiveDir)%(Filename)%(Extension)    </DestinationRelativePath>
          </FilesForPackagingFromProject>
        </ItemGroup>
  </Target>
</Project>

修改

我忘记了一个主要部分,将下面放在pubxml文件的底部:

 <PropertyGroup>
    <CopyAllFilesToSingleFolderForPackageDependsOn>
      CustomCollectFiles;
      $(CopyAllFilesToSingleFolderForPackageDependsOn);
    </CopyAllFilesToSingleFolderForPackageDependsOn>

    <CopyAllFilesToSingleFolderForMsdeployDependsOn>
      CustomCollectFiles;
      $(CopyAllFilesToSingleFolderForPackageDependsOn);
    </CopyAllFilesToSingleFolderForMsdeployDependsOn>
</PropertyGroup>

我没有得到该文件,但现在收到有关未找到文件的错误(我现在可以调试)。

1 个答案:

答案 0 :(得分:23)

我错过了两件事:

  1. 实际告诉它执行操作的第二个属性组。
  2. 路径不正确,必须使用项目目录路径
  3. 现在看起来像这样并起作用:

    <?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     http://go.microsoft.com/fwlink/?LinkID=208121. 
    -->
    <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <PropertyGroup>
        <WebPublishMethod>FileSystem</WebPublishMethod>
        <SiteUrlToLaunchAfterPublish />
            <publishUrl>\\myserver\wwwroot\apiproject</publishUrl>
        <DeleteExistingFiles>False</DeleteExistingFiles>
      </PropertyGroup>
    
     <Target Name="CustomCollectFiles">
        <ItemGroup>
          <_CustomFiles Include="$(MSBuildProjectDirectory)\bin\apiprojectv1.XML" />
          <FilesForPackagingFromProject Include="%(_CustomFiles.Identity)">
            <DestinationRelativePath>bin\%(RecursiveDir)%(Filename)%(Extension)    </DestinationRelativePath>
          </FilesForPackagingFromProject>
        </ItemGroup>
      </Target>
    
      <PropertyGroup>
        <CopyAllFilesToSingleFolderForPackageDependsOn>
          CustomCollectFiles;
          $(CopyAllFilesToSingleFolderForPackageDependsOn);
        </CopyAllFilesToSingleFolderForPackageDependsOn>
    
        <CopyAllFilesToSingleFolderForMsdeployDependsOn>
          CustomCollectFiles;
          $(CopyAllFilesToSingleFolderForPackageDependsOn);
        </CopyAllFilesToSingleFolderForMsdeployDependsOn>
    </PropertyGroup>
    
    </Project>