How to get aspnet_compiler called from Visual Studio during build?

I want Visual Studio to precompile my ASP.NET application , which is used as a payload in the Azure web role. So I found this post explaining how to call aspnet_compiler to check the views.

I tried adding the following to the post-build event of my ASP.NET application:

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

or alternatively this (application name is explicit):

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

In both cases, when the assembly is being performed, I see the following in the output of the assembly:

 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 obviously, no pre-compilation happens because if I change any .aspx or .cshtml Build Action file to None, it doesn’t get into the Azure service package and the view no longer opens after the package is deployed to Azure .

How to configure aspnet_compiler for precompilation from Visual Studio?

+7
source share
3 answers

If you want to use the Asp.NET compiler in your Visual Studio / msbuild, you can add the AspNetCompiler Define the project file (.csproj / .vbproj) and set MvcBuildViews to 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 can also set the TargetPath attribute to specify the destination directory.

AfterTargets="build" is similar to a "post build event". See Assembly target for more information.

+4
source

Integrate ASPX Compilation in Visual Studio

One of the principles that I insist on is always to try to build my clean environment and simulate the installation, as if it were done using QA. Recently, I noticed that I continue to fall for errors hidden deep in aspx files. So why not use the old and familiar aspnet_compiler.exe tool? It is located in the folder C: \ WINDOWS \ Microsoft.NET \ Framework \ v2.0.50727 and is quite simple to use.

As a complement to VS add-ins, I started thinking of a terrific add-in that will integrate into VS and listen to build events and display the results in the output panel. Heck, why not add some coffee-making options?

It took me about 10 minutes of a search query to stumble upon this blog. Mike Hadlow had a genius in its simplicity. Use the POST STORY EVENT! All I have 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) \"

Now all that remains is to make the process of adding this line for each web project in our team automatically.

I only have an add-on for this :)

enter the link here

+2
source

The answer from Matej was useful to me, but I could not use it as it is, and still get it working both for local assemblies in Visual Studio and for automatic assemblies via TFS.

I had to add additional msbuild settings. In fact, I had two different scenarios. One of the projects was a web application embedded in the _PublishedWebsites folder, and one of them was an MVC web application that was not embedded in the _PublishedWebsites folder.

First add the following if it is not already in the project file:

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

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

For one WITHOUT _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 "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> 
0
source

All Articles