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

sha*_*oth 7 .net asp.net azure visual-studio-2010 azure-web-roles

我希望Visual Studio 预编译我的ASP.NET应用程序,该应用程序用作Azure Web角色有效负载.所以我发现这篇文章解释了如何调用aspnet_compiler来验证视图.

我尝试将以下内容添加到我的ASP.NET应用程序的"post-build event"中:

call "%VS100COMNTOOLS%\vsvars32.bat"
aspnet_compiler -v / -p $(ProjectDir)
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

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

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

Mat*_*tej 4

如果您想在 Visual Studio / msbuild 中使用 Asp.NET 编译器,则可以将 AspNetCompiler任务添加到项目文件 (.csproj/.vbproj) 并设置MvcBuildViewstrue.

例子:

<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>
Run Code Online (Sandbox Code Playgroud)

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

AfterTargets="build"类似于“构建后事件”。有关更多信息,请参阅目标构建顺序。