简体   繁体   English

如何在构建过程中从Visual Studio调用aspnet_compiler?

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

I want Visual Studio to precompile my ASP.NET application which is used as an Azure web role payload. 我希望Visual Studio 预编译用作Azure Web角色有效负载的ASP.NET应用程序 So I've found this post that explains how to call aspnet_compiler to validate views. 因此,我发现这篇文章解释了如何调用aspnet_compiler来验证视图。

I tried to add the following to "post-build event" of my ASP.NET application: 我试图将以下内容添加到我的ASP.NET应用程序的“生成后事件”中:

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

or alternatively this (application name specified explicitly): 或替代地(明确指定的应用程序名称):

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

In both cases when the build runs I see the following in the build output: 在两种情况下,当构建运行时,我都会在构建输出中看到以下内容:

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

and clearly no precompilation happens because if I change any .aspx or .cshtml file "Build Action" to "None" it doesn't get to the Azure service package and the view no longer opens once the package is deployed to Azure. 并且显然不会进行预编译,因为如果我将任何.aspx或.cshtml文件“生成操作”更改为“无”,它将不会进入Azure服务程序包,并且一旦将程序包部署到Azure,视图将不再打开。

How do I setup aspnet_compiler for precompiling from within Visual Studio? 如何在Visual Studio中设置aspnet_compiler以进行预编译?

If you want to use Asp.NET Compiler within your Visual Studio / msbuild then you can add AspNetCompiler Task to your project file (.csproj/.vbproj) and set MvcBuildViews to true . 如果要在Visual Studio / msbuild中使用Asp.NET编译器,则可以将AspNetCompiler任务添加到项目文件(.csproj / .vbproj),并将MvcBuildViews设置为true

Example: 例:

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

You may also set TargetPath attribute to specify destination directory. 您也可以设置TargetPath属性以指定目标目录。

AfterTargets="build" is similar to "post-build event". AfterTargets="build"与“ post-build event”类似。 See Target Build Order for more. 有关更多信息,请参见目标构建顺序

Integrate ASPX compilation into Visual Studio 将ASPX编译集成到Visual Studio中

One of the principles I insist on is to always try my build on a clean environment and simulate installation as if it was done by QA. 我坚持的原则之一是始终在干净的环境中尝试构建并模拟安装,就像由QA进行的那样。 Lately I've noticed that I keep falling on errors hidden deep in the aspx files. 最近,我注意到我不断遇到隐藏在aspx文件深处的错误。 So, why not using the old and familiar aspnet_compiler.exe tool? 那么,为什么不使用旧的和熟悉的aspnet_compiler.exe工具呢? It is located at C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727 and it is quite easy to use. 它位于C:\\ WINDOWS \\ Microsoft.NET \\ Framework \\ v2.0.50727,非常易于使用。

As a VS add-ins freak I've started thinking on an amazing add-in that will integrate to the VS and will listen to build events and display the results at the output pane. 作为VS加载项的怪胎,我已经开始考虑将一个令人惊奇的加载项集成到VS,并将侦听构建事件并在输出窗格中显示结果。 Heck, why not add some coffee serving capabilities? 哎呀,为什么不增加一些咖啡服务功能呢?

It took me about 10 minutes of googling to stumble on this blog. 我花了大约10分钟的时间来搜寻该博客。 Mike Hadlow had a genius in its simplicity idea. Mike Hadlow的简洁想法颇具天赋。 Use the POST BUILD EVENT! 使用开机自检事件! All I need to do is put the following line in the post build event: C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727\\aspnet_compiler.exe -v / -p "$(ProjectDir)\\" 我需要做的就是将以下行放在post build事件中:C:\\ WINDOWS \\ Microsoft.NET \\ Framework \\ v2.0.50727 \\ aspnet_compiler.exe -v / -p“ $(ProjectDir)\\”

Now, All that is left is to make the process of adding this line to each and every web project in our team to be automatic. 现在,剩下的就是使此行自动添加到我们团队中的每个Web项目的过程。

I have just the add-in for that :) 我只有这个插件:)

enter link description here 在此处输入链接说明

The answer from Matej was helpful for me, but I was not able to use it as-is and still get it to work for both local builds within Visual Studio and automated builds via TFS. Matej的答案对我很有帮助,但是我无法按原样使用它,但仍然无法将其用于Visual Studio中的本地生成和通过TFS进行的自动生成。

I had to add some extra msbuild settings. 我必须添加一些额外的msbuild设置。 Actually, there were 2 different scenarios that I had. 实际上,我有两种不同的情况。 One project was an Web App that built into the _PublishedWebsites folder and one was an MVC Web App that did not build into the _PublishedWebsites folder. 一个项目是内置在_PublishedWebsites文件夹中的Web应用程序,而另一个项目是未内置在_PublishedWebsites文件夹中的MVC Web应用程序。

First, add the following if it is not already in your project file: 首先,如果您的项目文件中还没有添加以下内容:

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

For the one WITH _PublishedWebsites: 对于具有_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>

For the one WITHOUT _PublishedWebsites: 对于一个没有_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