SharePoint项目中的AspNetCompiler任务

时间:2014-06-28 07:32:28

标签: asp.net sharepoint sharepoint-2010 aspnet-compiler

我想知道在buildpoint解决方案中构建之后如何编译aspx文件以查看运行时错误,例如:缺少资源(使用资源翻译页面时)。

当我将AspNetCompiler任务添加到我的csproj中时(仅显示项目文件的结尾):

...
<PropertyGroup>
    <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
    <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
  </PropertyGroup>
  <Import Project="$(VSToolsPath)\SharePointTools\Microsoft.VisualStudio.SharePoint.targets" Condition="'$(VSToolsPath)' != ''" />
  <Target Name="AfterBuild">
    <AspNetCompiler VirtualPath="temp" PhysicalPath="$(ProjectDir)" />
    <!-- ToolPath="C:\Windows\Microsoft.NET\Framework64\v2.0.50727" -->
  </Target>
  <PropertyGroup>
    <PostBuildEvent>
    </PostBuildEvent>
  </PropertyGroup>
</Project>

,然后在构建项目时,我在aspx文件中收到以下错误:

Could not load file or assembly '$SharePoint.Project.AssemblyFullName$' or one of its dependencies. The system cannot find the file specified.

显然,asp编译器试图在项目目录中构建没有替换标记的aspx文件。但是,我无法完成任务,所以它会在更换令牌的情况下运行。我对MSBuild的了解有限,这可能是问题所在。

2 个答案:

答案 0 :(得分:0)

进行令牌替换的唯一方法是创建wsp包,因为替换发生在那里。然后解决方案是创建wsp,将其解压缩到目标文件夹,创建一个名为&#34; bin&#34;的新目录,将dll移动到bin目录,然后在目标文件夹上运行aspnet编译器。整个过程可以在MSBUild(csproj)文件中完成。这是我的解决方案 - 在csproj文件的末尾,在导入sharepoint目标之后:

  <Import Project="$(VSToolsPath)\SharePointTools\Microsoft.VisualStudio.SharePoint.targets" Condition="'$(VSToolsPath)' != ''" />
  <PropertyGroup>
    <!-- schedule creating the package in the build, the task is defined in sharepoint targets -->
    <BuildDependsOn>$(BuildDependsOn);CreatePackage</BuildDependsOn>
    <PostBuildEvent>
    </PostBuildEvent>

    <!-- define file/folder properties -->
    <ExpandDest>$(TargetDir)_wsp</ExpandDest>
    <WspBinPath>$(ExpandDest)\bin</WspBinPath>
    <WspPath>$(TargetDir)MyProject.wsp</WspPath>    
  </PropertyGroup>

  <!-- override "after build" target, it must depend on the package creation, 
       which ensures that the target runs after we have the wsp package ready -->
  <Target Name="AfterBuild" DependsOnTargets="CreatePackage">
    <!-- create the folder where we unpack our wsp -->
    <MakeDir Directories="$(ExpandDest)" />    
    <!-- use expand to unpack the wsp -->
    <Message Text="$(ExpandWsp)" Importance="high" />
    <Exec Command="expand &quot;$(WspPath)&quot; -F:* &quot;$(ExpandDest)&quot;" />

    <!-- create the "bin" folder -->
    <MakeDir Directories="$(WspBinPath)" />
    <!-- move all dlls and xmls from the root to the bin folder -->
    <Exec Command="move /Y &quot;$(ExpandDest)\*.dll&quot; &quot;$(WspBinPath)&quot;" />
    <Exec Command="move /Y &quot;$(ExpandDest)\*.xml&quot; &quot;$(WspBinPath)&quot;" />

    <!-- run the aspnet compiler on the wsp folder,
         the tool path param ensures that .net 2 compiler will be used,
         we need that because we compile sharepoint 2010 which is .net 3.5
         and the latest aspnet compiler for .net 3.5 is in .net 2 -->
    <AspNetCompiler 
      VirtualPath="/"
      PhysicalPath="$(ExpandDest)"
      Clean="true"
      ContinueOnError="false"
      ToolPath="C:\Windows\Microsoft.NET\Framework\v2.0.50727" />
  </Target>
</Project>

编辑:这个过程有点复杂。根据您自己的项目,您必须将一些文件移动到特定文件夹。基本上,请按照编译器的说法进行操作。

答案 1 :(得分:0)

您可能知道在创建wsp文件之前已将所需文件(作为Visual Studio中打包过程的一部分)复制到文件夹中,您可以轻松指向该文件夹,因此无需执行所有这些文件夹创建,移动和提取。因此,恕我直言的最佳解决方案是仅使用以下内容:

<Target Name="AfterBuild" DependsOnTargets="CreatePackage">
    <Message Text="Validating Asp.Net files (PhysicalPath: $(LayoutPath), ToolPath: $(FrameworkDir)$(FrameworkVersion))..." Importance="high" />
    <AspNetCompiler 
        VirtualPath="/" 
        PhysicalPath="$(LayoutPath)" 
        Clean="true" ContinueOnError="false" 
        ToolPath="$(FrameworkDir)$(FrameworkVersion)" />
    <Message Text="Asp.Net validation is complete" Importance="high" />