Publishing ASP.Net Applications from the Command Line

Visual Studio 2005 has a built-in Publish command which generates a set of (more or less) randomly named DLLs, instead of a single DLL that was generated back in the good old days of ASP.Net 1.1.

An add-on, the VS 2005 Web Deployment Projects, offers the functionality to generate a single assembly for the web application, among other useful things, and includes the aspnet_merge tool.

The publishing of an application usually always takes the same steps with the same options, and can take a couple of minutes (depending on your project). Since repetitive tasks are a source for errors, I wanted to automate this procedure.

I wanted to take a look under the hood and create a batch file which compiles a web application from source files using aspnet_compiler and then calls aspnet_merge.

The first problem I met was that aspnet_merge was not in the PATH. Rather it is being installed into

C:\Program Files\MSBuild\Microsoft\WebDeployment\v8.0

which is a localized path, so you either change the PATH variable, or copy aspnet_merge into a directory in the PATH.

I created a batch file called publish.cmd, which looks like this:

if "%1"=="" goto errnoparam
if not exist c:\inetpub\wwwroot\%1\nul goto errnotfound

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

set base=c:\archive\publish\%1
if not exist %base%\nul md %base%
rd /s /q %base%

echo compiling to %base% 
aspnet_compiler -v /%1 -f -d %base%
aspnet_merge %base% -w %1 -debug 

echo web compiled to %base%
goto end

echo usage: %0 web
goto end

echo web %1 not found
goto end


First, the script checks whether the name for the web application is provided, and whether it is a valid existing name. Next, the VS2005 environment variables are initialized.

Finally it calculates the name of the output directory, and invokes aspnet_compiler and aspnet_merge.

The result:

  • beautifully named DLL files
  • empty .aspx and .ascx files (so nobody steals your markup😉 )
  • ugly .compiled files in the bin directory (cannot be deleted without breaking the application)

4 Responses to Publishing ASP.Net Applications from the Command Line

  1. […] Publish from command line using aspnet_compiler and aspnet_merge […]

  2. […] 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). […]

  3. […] When developing database applications, I usually start out with the database schema and have the necessary C# code (data classes, procedure calls, application-specific constants) generated by a couple of stored procedures. For my own projects, I create a batch file to build the various parts of the solution, all the way from code generation to publishing the application. […]

  4. […] build process invokes both aspnet_compiler and aspnet_merge where aspnet_compiler requires all markup files to be present in the project directory, as it seems […]

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: