As usual, I am going to jump immediately to the code.
You need first Wix Microsoft Installer tools which you can download it from here.


We are going to write a batch code that will do the following:

  1. Compile the asp.net web application.
  2. Publish the Asp.net web application to a folder.
  3. Compile files that contains Wix code (wsx files), which compress all the files in the publish folder resulted from step 2, and add it into msi file.
  4. The Wix code will create a folder under c:\inetpub\wwwroot{folder name=””}, and create a web application under the default web site, and associate it with an application pool that run .NET framework version 4.0.

Let’s start by writing the batch file:

set publishFolder=publish\
C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild /p:Configuration=Release /t:ReBuild ..\thewebsolution.sln
C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild /t:ResolveReferences;_CopyWebApplication 
  /p:Configuration=Release; OutDir=..\Setup\%publishFolder%\bin\;
  WebProjectOutputDir=%publishFolder% myWeb.vbproj


The above code will compile the solution and deploy the output of the web application myweb.vbproj into a folder called “Publish”.
Next let’s write the main wix code that will build the msi file:

<wix xmlns:iis="http://schemas.microsoft.com/wix/IIsExtension"
  xmlns:netfx="http://schemas.microsoft.com/wix/NetFxExtension" 
  xmlns:util="http://schemas.microsoft.com/wix/UtilExtension"
  xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <product id="{FA1B9338-B6F6-413e-B67F-86CA8BCED6E8}" language="1033" manufacturer="my company name" 
    name="my.website.install" upgradecode="{E5C9F391-5787-4fd1-81E6-D1A4A91D226D}" version="1.0.0.0">
      <package compressed="yes" installerversion="200">
        <media cabinet="mywebsite.cab" embedcab="yes" id="1">
          <propertyref id="NETFRAMEWORK40FULL">
            <Directory Id="TARGETDIR" Name="SourceDir">
              <!-- Install stuff into wwwroot folder. -->
              <Directory Id="WWWROOT">
                <Directory Id="INSTALLLOCATION" Name="MyWebDirectory">
                  <!-- TODO: to add content of published web site -->
                </Directory>
              </Directory>
            </Directory>
            <!-- TODO: add iis configuration -->
            <property id="WWWROOT">c:\inetpub\wwwroot\</property>
          </propertyref>
        </media>
      </package>
    </product>
</wix>


The “Product” tag will define the output msi, and they are the attributes that will be stored in the result msi file.
The previous xml, will generate only an msi that will create a directory called “MyWebDirectory” inside c:\inetpub\wwwroot.
Next, we want to add the content of the “Publish” directory into that folder. To do that, we need to add the content of the publish folder as xml input.
There is a tool that help us “harvest” the folder content and make it into an xml file. The following command line that we will add it to the batch file, will do that and harvest all folder content into file called WebSiteContent.wxs.

# this is continue for the previous batch
"C:\Program Files (x86)\Windows Installer XML v3.5\bin\heat" dir %publishFolder% 
    -dr MYWEBWEBSITE -ke -srd -cg MyWebWebComponents -var var.publishDir 
    -gg -out WebSiteContent.wxs 

Next, we will add the WebSiteContent.wxs into the Product.wxs tag. In the product.wxs in the previous XML there is a comment:

<!-- TODO: to add content of published web site -->

We replace it with the following:

<Directory Id="MYWEBWEBSITE" Name="Website">


Now, we will add the IIS configuration into a second file called IisConfiguration.wxs.

<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi" xmlns:iis="http://schemas.microsoft.com/wix/IIsExtension" 
  xmlns:util="http://schemas.microsoft.com/wix/UtilExtension">
  <Fragment>
    <!-- Install to default web site -->
    <iis:WebSite Id="DefaultWebSite" Description='Default Web Site'>
      <iis:WebAddress Id="AllUnassigned" Port="80" />
    </iis:WebSite>
    <DirectoryRef Id="MYWEBWEBSITE">
      <!-- Configuring app pool -->
      <Component Id="MyWebAppPoolCmp" Guid="{009052A8-19AE-452e-AE34-6DC8E929DA08}" 
        KeyPath="yes" Permanent="yes">
        <iis:WebAppPool Id="MyWebAppPool" Name="ASP.NET v4.0"/>
      </Component>
      <!-- Configure virtual dir -->
      <Component Id="MyWebVirtualDirCmp" Guid="{751DEB01-ECC1-48ff-869A-65BCEE9E0528}" KeyPath="yes" >
        <iis:WebVirtualDir Id="MyWebVirtualDir" Directory="MYWEBWEBSITE" WebSite="DefaultWebSite">
          <iis:WebDirProperties Id="MyWebVirtDirProperties" AnonymousAccess="no" 
            BasicAuthentication="no" WindowsAuthentication="yes" />
          <iis:WebApplication Id="MyWebWebApplication" Name="GatineauWebmap" WebAppPool="MyWebAppPool"/>
        </iis:WebVirtualDir>
      </Component>
    </DirectoryRef>
    <ComponentGroup Id="MyWebIssConfiguration">
      <ComponentRef Id="MyWebVirtualDirCmp" />
      <ComponentRef Id="MyWebAppPoolCmp" />
    </ComponentGroup>
  </Fragment>
</Wix>


So, what remains is to add a reference to the code inside IisConfiguration.wxs, inside the product.wxs.
Now replace the line:

<!-- TODO: add iis configuration --> 
<Feature Id="Complete" Title="Gatineau - Gatineau Webmap application" Level="1"
  Display="expand" ConfigurableDirectory="INSTALLLOCATION">
  <!-- Main content of the Complete feature. -->
  <Feature Id="MainContent" Title="Gatineau Website" Description="The website content" Level="1">
    <!-- Include IIS Configuration. -->
    <ComponentGroupRef Id="MyWebIssConfiguration" />
    <!-- Include web content. -->
    <ComponentGroupRef Id="MyWebWebComponents" />
    <!-- Perform changes in the web.config file. -->
    <!---->
  </Feature>
</Feature>

And now we add wix commands in the batch file to compile and produce the msi file

"C:\Program Files (x86)\Windows Installer XML v3.5\bin\candle" 
  -ext WixIISExtension -ext WixUtilExtension -ext WiXNetFxExtension -dpublishDir=%publishFolder% 
  -dMyWebResourceDir=. IisConfiguration.wxs Product.wxs WebSiteContent.wxs
"C:\Program Files (x86)\Windows Installer XML v3.5\bin\light" -ext WixUIExtension 
  -ext WixIISExtension -ext WixUtilExtension -ext WiXNetFxExtension 
  -out bin\Release\Gatineau.Webmap.Setup.msi Product.wixobj WebSiteContent.wixobj IisConfiguration.wixobj