如何在构建期间从Visual Studio调用aspnet_compiler?

时间:2012-09-20 11:07:39

标签: asp.net .net visual-studio-2010 azure azure-web-roles

我想要Visual Studio到precompile my ASP.NET application,它用作Azure Web角色有效负载。所以我发现this post解释了如何调用aspnet_compiler来验证视图。

我尝试将以下内容添加到"构建后事件"我的ASP.NET应用程序:

call "%VS100COMNTOOLS%\vsvars32.bat"
aspnet_compiler -v / -p $(ProjectDir)

或者这个(显式指定的应用程序名称):

call "%VS100COMNTOOLS%\vsvars32.bat"
aspnet_compiler -v /ASP.NET-Application-ProjectNameHere -p $(ProjectDir)

在构建运行的两种情况下,我在构建输出中看到以下内容:

Setting environment for using Microsoft Visual Studio 2010 x86 tools.
Utility to precompile an ASP.NET application
Copyright (C) Microsoft Corporation. All rights reserved.

并且显然没有预编译,因为如果我更改任何.aspx或.cshtml文件" Build Action"到"无"它无法访问Azure服务包,并且一旦将程序包部署到Azure,该视图就不再打开。

如何在Visual Studio中设置aspnet_compiler进行预编译?

3 个答案:

答案 0 :(得分:4)

如果要在Visual Studio / msbuild中使用Asp.NET Compiler,则可以添加  AspNetCompiler任务到项目文件(.csproj / .vbproj)并将MvcBuildViews设置为true

示例:

<Project>
  <PropertyGroup>
    <MvcBuildViews>true</MvcBuildViews>
  </PropertyGroup>
  <!-- ... -->
  <Target Name="PrecompileWeb" AfterTargets="build" Condition="'$(MvcBuildViews)'=='true'">
    <Message Text="Starting AspNetCompiler for $(ProjectDir)" Importance="high" />
    <AspNetCompiler
        VirtualPath="temp"
        PhysicalPath="$(WebProjectOutputDir)"
        Force="true"
    />
  </Target>
  <!-- ... -->
</Project>

您也可以设置TargetPath属性以指定目标目录。

AfterTargets="build"类似于“post-build event”。有关详情,请参阅Target Build Order

答案 1 :(得分:2)

将ASPX编译集成到Visual Studio中

我坚持的原则之一是始终在干净的环境中尝试构建并模拟安装,就好像它是由QA完成的。最近我注意到我一直在隐藏在aspx文件深处隐藏的错误。那么,为什么不使用旧的和熟悉的aspnet_compiler.exe工具呢?它位于C:\ WINDOWS \ Microsoft.NET \ Framework \ v2.0.50727,它非常易于使用。

作为一个VS加载项狂,我开始考虑一个可以集成到VS的惊人加载项,并将监听构建事件并在输出窗格中显示结果。哎,为什么不加一些咖啡服务?

我花了大约10分钟的谷歌搜索才能在这个博客上绊倒。 Mike Hadlow在其简约理念中拥有天才。使用POST BUILD EVENT! 我需要做的就是在post build事件中添加以下行:C:\ WINDOWS \ Microsoft.NET \ Framework \ v2.0.50727 \ aspnet_compiler.exe -v / -p“$(ProjectDir)\”

现在,剩下的就是将我们团队中每个Web项目添加此行的过程自动化。

我只有加载项:)

enter link description here

答案 2 :(得分:0)

Matej的答案对我很有帮助,但是我无法按原样使用它,仍然能够在Visual Studio中使用本地构建并通过TFS实现自动构建。

我不得不添加一些额外的msbuild设置。实际上,我有两种不同的场景。一个项目是内置于_PublishedWebsites文件夹的Web应用程序,另一个是未构建到_PublishedWebsites文件夹的MVC Web应用程序。

首先,如果项目文件中尚未添加以下内容,请添加以下内容:

  <PropertyGroup>
    <MvcBuildViews>true</MvcBuildViews>
  </PropertyGroup>

对于一个WITH _PublishedWebsites:

  <Choose>
    <When Condition="'$(BuildingInsideVisualStudio)' == true">
      <PropertyGroup>
        <AspNetCompilerPhysicalPath>$(ProjectDir)</AspNetCompilerPhysicalPath>
      </PropertyGroup>
    </When>
    <Otherwise>
      <PropertyGroup>
        <AspNetCompilerPhysicalPath>$(WebProjectOutputDir)</AspNetCompilerPhysicalPath>
      </PropertyGroup>
    </Otherwise>
  </Choose>
  <Target Name="PrecompileWeb" AfterTargets="build" Condition="'$(MvcBuildViews)'=='true'">
    <!-- aspnet_compiler.exe needs to be run on the folder that has the aspx files and the "bin" subfolder.
            When running locally, the value needs to be the project directory, which is $(ProjectDir). 
            When running the TFS build, the value needs to be (BuildFolder)\(ProjectName)\_PublishedWebsites\(ProjectName).
            The $(AspNetCompilerPhysicalPath) will hold the correct value for both types of builds.
    -->
    <Message Text="Starting AspNetCompiler for $(ProjectName) at $(AspNetCompilerPhysicalPath)" Importance="high" />
    <AspNetCompiler 
        VirtualPath="/" 
        PhysicalPath="$(AspNetCompilerPhysicalPath)" 
        TargetPath="$(AspNetCompilerPhysicalPath)\bin_precompile" 
        Force="true" 
        />
  </Target>

对于没有_PublishedWebsites的人:

  <Choose>
    <When Condition="'$(BuildingInsideVisualStudio)' == true">
      <PropertyGroup>
        <AspNetCompiler_CopyFilesFirst>false</AspNetCompiler_CopyFilesFirst>
      </PropertyGroup>
    </When>
    <Otherwise>
      <PropertyGroup>
        <AspNetCompiler_CopyFilesFirst>true</AspNetCompiler_CopyFilesFirst>
      </PropertyGroup>
      <ItemGroup>
        <AllOutputFiles Include="$(OutDir)\\**\*.*" />
      </ItemGroup>
    </Otherwise>
  </Choose>
  <Target Name="PrecompileWeb" AfterTargets="build" Condition="'$(MvcBuildViews)'=='true'">
    <!-- aspnet_compiler.exe needs to be run on the folder that has the cshtml files and the "bin" subfolder. I could not find a setting that was appropriate for both. 
            When running locally, the value needs to be the project directory, which is $(ProjectDir). 
            When running the TFS build, there is no folder that matches both of those criteria. 
            So first we will copy the output into the source code folder's "bin" subfolder, 
            then run it against the source $(ProjectDir), the same as if we were building locally.
    -->
    <Message Text="Before running AspNetCompiler, copy files from $(OutDir) to $(ProjectDir)\bin" Importance="high" />
    <Exec Command="( robocopy.exe /mir  $(OutDir) $(ProjectDir)\bin ) ^&amp; IF %25ERRORLEVEL%25 LEQ 1 exit 0" Condition="'$(AspNetCompiler_CopyFilesFirst)'=='true'" />

    <Message Text="Starting AspNetCompiler for $(ProjectName) at $(ProjectDir)" Importance="high" />
    <AspNetCompiler 
        VirtualPath="/" 
        PhysicalPath="$(ProjectDir)" 
        TargetPath="$(ProjectDir)\bin_precompile" 
        Force="true" 
        />
  </Target>