Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. So I've found this post that explains how to call aspnet_compiler to validate views.

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

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.

How do I setup aspnet_compiler for precompiling from within Visual Studio?

like image 487
sharptooth Avatar asked Sep 20 '12 11:09

sharptooth


2 Answers

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.

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.

AfterTargets="build" is similar to "post-build event". See Target Build Order for more.

like image 123
Matej Avatar answered Sep 17 '22 16:09

Matej


Integrate ASPX compilation into 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. Lately I've noticed that I keep falling on errors hidden deep in the aspx files. So, why not using the old and familiar aspnet_compiler.exe tool? It is located at C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 and it is quite easy to use.

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. Heck, why not add some coffee serving capabilities?

It took me about 10 minutes of googling to stumble on this blog. Mike Hadlow had a genius in its simplicity idea. 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)\"

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.

I have just the add-in for that :)

enter link description here

like image 33
Muhammad Mubashir Avatar answered Sep 21 '22 16:09

Muhammad Mubashir