Modifying web.config file – Web setup project

In my previous post I talked about how to open a web setup project and add custom actions and user interfaces to it. Now I will give some examples of how to modify the web.config file that is copied in the virtual directory by our installer.

Most setups need to change the content of the web.config file for many reasons such as target machine properties, link updates etc. Setups may also require additional information from user to update the web.config accordingly.

For gathering input from user, there are several ways:

  • Using Web Setup Project’s User Interfaces

If a few text boxes are enough for you, the provided User Interfaces will satisfy your needs. You can pass the user entered parameters to your installer class.(Explained in previous post)

‘targetdir’ parameter keeps the web.config file path.

An important note: You should not try to modify the web.config during installation process. Use the AfterInstall event handler to make your updates.

  • Creating Your Own Forms Applciation

You can create a windows forms project and run it as a process at AfterInstall event handler.

run executable

To do this, you need to add the project output of the forms application you created to the web setup. After doing this, web setup will copy your executable to the target directory and you will be able to run it there.

The forms application can modify the web.config anyway you like.

  • ASP.Net configuration
If your web application is a asp.net application, then you can easily modify the web.config from the application itself. Prepare a web.config modification page and at codebehind update the web.config file like that:
The example code updates the AppSetting part of the web.config with the given key. But you can modify any section of the config file. See: more ways
Taylan

Visual Studio Web Setup Project

Web setup project is simply a visual studio setup project that helps web projects to be installed on a target machine’s IIS. Setup project and Web Setup project templates are available in Visual Studio.

open web setup project

     After you open a web setup project. Click on the project on solution explorer and press f4. Title of installer, icon, version, manufacter etc. can be set here.

properties window

     Since we are creating a web setup, we should have web project that is going to be published under IIS. Add an existing web project of yours in the web setup solution. After that right click on the web setup project Add->Project Output. In the pop-up dialog select your web project from the combo box at the top and select both ‘Primary Output’ and ‘Content Files’ in the list box. Note that only the dlls that are referenced by your web project will be copied into virtual directory.

Other dlls in your bin directory that are copied by a postbuid event or a manual copy/paste, will not be available in the VirtualDirectory/bin folder. A quick solution is to add reference to these dlls in your web project so that they will be recognized by web setup project as dependency and copied to target virtual directory.

add project output

User Interface :

Right click on the project on solution explorer and navigate to View->User Interace.

There are some template dialogs you can add to your installer. Right click on the Start and click Add Dialog.

Choose the template that fits your purpose and begin to edit the dialog through Properties window.

Set visibility of fields to false that are not needed. Edit#Property is the variable that is assigned with user’s input. We are going to use these values in next part.

dialog properties window

this configuration will look like that

text box dialog example

Custom Actions:

Custom actions are user defined executables, dlls or scripts which run by installer.  I will show a C# example custom action.

Right click on the solution and Add -> New Project -> Class Library. You can add an existing project too.

We need to have reference to System.Configuration.dll & System.Configuration.Install.dll. Now you can create your own Installer class and play with it.  Inherit Installer class and don’t forget the RunInstaller attribute.

Installer class example

     Now we need to add the project output to the setup project. Right click on the setup project Add -> Project Output. Select the Class library project and select Primary Output then click OK.

To add this class library project as a custom action to our web setup project. Right click on setup project View->CustomActions then Add Custom Action to Install. And press f4 after adding the custom action. You will see something like:

custom action properties

     We have a installer class and we need user inputs in that class. To provide these parameters write your parameters in the Custom Action Box above. In my case it is :

/targetdir=”[TARGETDIR]\” /targetvdir=”[TARGETVDIR]” /targetsite=”[TARGETSITE]” /sql=”[SQL_SERVERNAME]” /sqldbname=”[SQL_DB_NAME]” /sqluser=”[SQL_USERNAME]” /sqlpw=”[SQL_PASSWORD]” /

Retrieve these in your installer class :

retrieve parameters in custom action

That is a brief introduction to web setup projects.  I will talk about specific problems I encountered in my next posts.

Taylan