Publishing Web Application Projects from the Command Line (VS2008)

If you are a regular reader of this blog, you will know that one of the recurring topics is automatic builds of Visual Studio projects from command line or batch (see here, here, here).

I have written about the functionality of Web Deployment Projects for Visual Studio 2005, and after upgrading also downloaded Web Deployment Projects for Visual Studio 2008. Not much has changed in the meantime, but there are some noteworthy differences between Web Site Projects and Web Application Projects.

The main issue when compiling a Web App Project using aspnet_compiler (instead of msbuild) is that some files (such as the .csproj and .csproj.user files) and directories are copied to the output, as they do not exist in Web Site projects.

Thus the build+publish process should remove the files not required for execution (we are dealing with compiled applications, after all).

This is the updated batch file for building web application projects:

@echo off
if "%1"=="" goto errnoparam
if "%2"=="" goto errnoparam

if not exist %2\nul goto errnotfound

call "C:\Program Files\Microsoft Visual Studio 9.0\VC\vcvarsall.bat" x86
echo on 

set base=c:\path-to\publish\%1

if not exist %base%\nul md %base%

rd /s /q %base%

echo compiling to %base% 

aspnet_compiler -v /%1 -f -d -c -p %2 %base%

aspnet_merge %base% -w %1.pages -debug 

echo web compiled to %base%

pushd %base%
del *.csproj
del *.csproj.user
rd /s /q Code
rd /s /q obj
rd /s /q Properties
rd /s /q "Web References"
ren web.config web.publish.config


goto end


echo usage: %0 webname sourcepath
goto end


echo web %1 not found
goto end


The batch file takes two parameters: the assembly name and the path to the web app project (assuming that web app projects do not reside in the inetpub\wwwroot directory, as was the case with web site projects).

It calls aspnet_compiler to compile pages, controls, and code-behind, and aspnet_merge to merge all markup files (.aspx, .ascs, etc) into an assembly names *.pages. Afterwards it cleans up unnecessary files and directories, and renames web.config (to avoid accidentally overriding web.config in production environments).

Edit 14.9.2010: It seems the runtime requires directories originally containing .ascx files to be present in the published web, even though the directory is empty.

2 thoughts on “Publishing Web Application Projects from the Command Line (VS2008)

  1. Pingback: Registering UserControls in web.config, and Handling Nested UserControls « devioblog

  2. Pingback: Using Linked Files in ASP.Net Applications | devioblog

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.