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 thoughts on “Publishing ASP.Net Applications from the Command Line

  1. Pingback: Differences between Web Site Project and Web Application Project « devioblog

  2. Pingback: Publishing Web Application Projects from the Command Line (VS2008) « devioblog

  3. Pingback: Invoking Stored Procedures generating C# Code using T4 Templates « devioblog

  4. 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 )

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.