8

Azure Web ロール ペイロードとして使用されるASP.NET アプリケーションをVisual Studio でプリコンパイルしたいと考えています。そこで、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" を "None" に変更すると、Azure サービス パッケージに到達せず、パッケージが Azure にデプロイされるとビューが開かなくなるため、明らかにプリコンパイルは行われません。

Visual Studio 内からプリコンパイルするために aspnet_compiler をセットアップするにはどうすればよいですか?

4

3 に答える 3

4

Visual Studio / msbuild内でAsp.NETコンパイラを使用する場合は 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"「ビルド後のイベント」に似ています。詳細については、ターゲットビルドオーダーを参照してください。

于 2012-10-04T23:56:10.420 に答える
2

ASPX コンパイルを Visual Studio に統合する

私が主張する原則の 1 つは、ビルドを常にクリーンな環境で試し、QA によって行われたかのようにインストールをシミュレートすることです。最近、aspx ファイルの奥深くに隠されたエラーが発生し続けることに気付きました。では、古くて使い慣れた aspnet_compiler.exe ツールを使用してみませんか? C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 にあり、非常に使いやすいです。

VS アドイン フリークとして、私は VS に統合し、ビルド イベントをリッスンして結果を出力ペインに表示する素晴らしいアドインについて考え始めました。コーヒーを提供する機能を追加してみませんか?

このブログにたどり着くまで、グーグルで約10分かかりました。マイク・ハドロウは、そのシンプルなアイデアに天才的才能を持っていました。POST BUILD EVENTを使用してください!C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_compiler.exe -v / -p "$(ProjectDir)\" という行をポスト ビルド イベントに追加するだけです。

あとは、この行をチーム内のすべての Web プロジェクトに追加するプロセスを自動化するだけです。

私はそのためのアドインを持っています:)

ここにリンクの説明を入力

于 2013-03-06T12:06:06.463 に答える
1

Matej からの回答は役に立ちましたが、そのままでは使用できず、Visual Studio 内のローカル ビルドと TFS による自動ビルドの両方で動作させることができませんでした。

msbuild 設定を追加する必要がありました。実際には、2 つの異なるシナリオがありました。1 つのプロジェクトは _PublishedWebsites フォルダーに組み込まれた Web アプリで、もう 1 つは _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>
于 2016-03-10T19:31:08.723 に答える