簡體   English   中英

如何在構建過程中從Visual Studio調用aspnet_compiler?

[英]How to get aspnet_compiler invoked from Visual Studio during build?

我希望Visual Studio 預編譯用作Azure Web角色有效負載的ASP.NET應用程序 因此,我發現這篇文章解釋了如何調用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文件“生成操作”更改為“無”,它將不會進入Azure服務程序包,並且一旦將程序包部署到Azure,視圖將不再打開。

如何在Visual Studio中設置aspnet_compiler以進行預編譯?

如果要在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"與“ post-build event”類似。 有關更多信息,請參見目標構建順序

將ASPX編譯集成到Visual Studio中

我堅持的原則之一是始終在干凈的環境中嘗試構建並模擬安裝,就像由QA進行的那樣。 最近,我注意到我不斷遇到隱藏在aspx文件深處的錯誤。 那么,為什么不使用舊的和熟悉的aspnet_compiler.exe工具呢? 它位於C:\\ WINDOWS \\ Microsoft.NET \\ Framework \\ v2.0.50727,非常易於使用。

作為VS加載項的怪胎,我已經開始考慮將一個令人驚奇的加載項集成到VS,並將偵聽構建事件並在輸出窗格中顯示結果。 哎呀,為什么不增加一些咖啡服務功能呢?

我花了大約10分鍾的時間來搜尋該博客。 Mike Hadlow的簡潔想法頗具天賦。 使用開機自檢事件! 我需要做的就是將以下行放在post build事件中:C:\\ WINDOWS \\ Microsoft.NET \\ Framework \\ v2.0.50727 \\ aspnet_compiler.exe -v / -p“ $(ProjectDir)\\”

現在,剩下的就是使此行自動添加到我們團隊中的每個Web項目的過程。

我只有這個插件:)

在此處輸入鏈接說明

Matej的答案對我很有幫助,但是我無法按原樣使用它,但仍然無法將其用於Visual Studio中的本地生成和通過TFS進行的自動生成。

我必須添加一些額外的msbuild設置。 實際上,我有兩種不同的情況。 一個項目是內置在_PublishedWebsites文件夾中的Web應用程序,而另一個項目是未內置在_PublishedWebsites文件夾中的MVC Web應用程序。

首先,如果您的項目文件中還沒有添加以下內容:

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

對於具有_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>

對於一個沒有_Published網站的網站:

  <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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM