Monday, May 26, 2008

Compiling aspx templates using aspnet_compiler

The default build process for aspx files is a problem in web site development. By default they only build on request, so nasty compile time errors can lay hidden until you actually hit the problem page when looking at the site. This is a real pain; when I hit F6, I want everything to build.

I've only recently come across the command line tool aspnet_compiler. It simply pre-compiles all the aspx pages in your web application. You can run it as a post build event which means all your aspx templates get built each time:


Now when you build, you get aspx errors reported too. Clicking on the errors takes you to the correct place in the aspx file, just like with regular .cs code:


Now is this common knowledge that I've just somehow missed out on. It seems strange that I had to work out this trick by myself?


Steve said...

Hi Mike,

Have you looked at Web Deployment Projects? IIRC it does theaspnet_compiler for you an includes a bunch of other features..

Mike Hadlow said...

Thanks Steve, I'll check that out.

Sebastien Lambla said...

You can also simply add the AspNetCompiler msbuild task to your csproj file.

That said, on our build servers I first call the publish tasks on the web app project, copy the content over, and run AspNetCompiler. Otherwise, things not in the csproj end up being compiled too.

Steve said...

Thats true Sebastien..

I actually got some time yesterday to look at Web Deployment projects and they seem to do exactly what you describe - copy over files not in the csproj - which for me means Resharper's cache directory too! Have to be careful not to copy that one up to the live server..

On the other hand features like Web.config section replacement are pretty handy (though I expect can be achievied without too much of a headache through a custom msbuild task)

Mike Hadlow said...

Thanks Seb, I've tried adding the AspNetCompiler msbuild task to my project and it's a much neater solution than using the post build event.

There's an interesting post by K, Scott Allen here:
on the subject.

Unknown said...

OMG! Just what I needed! Thank you!

Shrikant said...

hey thnx buddy...very helpful...I had been wathcing for something of this sort...

Shrikant said...
This comment has been removed by the author.
Shrikant said...

if u can help....Im tryin run the same aspnet_compiler command thru v4.0.XXX,but it is erring me like error ASPCONFIG: Could not load file or assembly 'System.Web.ApplicationServices
, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of i
ts dependencies. The system cannot find the file specified.

plz see if u know smthng abt this.

Gilles Leblanc said...

Using the AspNetCompiler, can I use it to compile the aspx files locally on my computer and not only on the build server?

If so, how do I do this?

Or to do this locally must I use the post build event over the csproj modification?