Introduction
We want the user to set the physical path for our Web application IIS virtual directory, during deployment. Since the Web Setup project does not allow this, our idea is to use the Application Setup project to create the physical path, and content, and later with a Custom Action, create the virtual directory, using IIS Admin Objects.
Background
In a previous article posted, we talked about how to set the IIS Virtual Directory Authentication Settings. Take a look at the article if you also want to change this.
Using the code
Start by creating a new Web Application project, maybe you have already one to deploy. Create a new Web Form and name it Default.aspx.
Now, create a new Visual Basic Class Library called CustomAction. Delete Class1.vb. Add a new class, based on the Installer Class template, named CustomAction.vb to the project. (Right click Add->Add class... Select Installer Class from templates). Open it and switch to Code View.
Add:
Imports System.IO
to the CustomAction.vb class Imports section.
Now add the following method overrides for the installer class:
This is the method called after the Setup Deployment Project installer predefined stuff. It creates the new virtual directory in the default IIS server, set its physical path to the application folder, creates the Web application as In Process, and sets the default document page. It also shows hot to save the installer state to be read during uninstall:
Public Overrides Sub Install(ByVal stateSaver As _
System.Collections.IDictionary)
MyBase.Install(stateSaver)
Dim Asm As System.Reflection.Assembly = _
System.Reflection.Assembly.GetExecutingAssembly
Dim DirLocation As String = Path.GetDirectoryName(Asm.Location)
Dim vRoot = GetObject("IIS://LocalHost/W3svc/1/Root")
Dim vDir = vRoot.Create("IIsWebVirtualDir", _
Me.Context.Parameters.Item("dir"))
vDir.Path = DirLocation
vDir.DefaultDoc = "Default.aspx"
vDir.SetInfo()
vDir.AppCreate(True)
stateSaver.Add("VirtualDir", _
Me.Context.Parameters.Item("dir"))
End Sub
This is the method called after the Setup Deployment Project unnistaller predefined stuff. It gets the VirtualDir
key value saved in the installer state, and uses it to delete the virtual directory from IIS during un-installation:
Public Overrides Sub Uninstall(ByVal mySavedState As IDictionary)
MyBase.Uninstall(mySavedState)
Try
Dim vRoot = GetObject("IIS://LocalHost/W3svc/1/Root")
vRoot.Delete("IIsWebVirtualDir", mySavedState("VirtualDir"))
Catch ex As Exception
End Try
End Sub
And this is the method called after all the setup deployment steps are executed, it opens the Web application default page in Internet Explorer:
Public Overrides Sub Commit(ByVal mySavedState As IDictionary)
MyBase.Commit(mySavedState)
Try
Dim P As New Process
P.Start("iexplore.exe", "http://localhost/" & _
Me.Context.Parameters.Item("dir") & "/")
Catch ex As Exception
End Try
End Sub
Save CustomAction.vb. Now, add the "Web To Install" Setup Project to the solution. (Note, we use an Application Setup Project, not a Web Setup Project, for this deployment.)
Add the Primary Output and Content Files of the WebToIstall project to the Setup Project output. (Right click -> Add -> Project output...)
Repeat the operation to add the CustomAction project Primary Output.
Add the CustomActions custom actions to the Setup Project:
- Install: Right click -> View -> Custom Actions. In the Custom Actions view, right click Install -> Add Custom Action. Double click "Application Folder". Select CustomAction Primary Output. Click OK. Right click the new custom action -> Properties window. Set the
CustomActionData
property to "/dir=[VIRTDIR]" (without the ""s). - Commit: Right click -> View -> Custom Actions. In the Custom Actions view, right click Commit-> Add Custom Action. Double click "Application Folder". Select CustomAction Primary Output. Click OK. Right click the new custom action -> Properties window. Set the
CustomActionData
property to "/dir=[VIRTDIR]" (without the ""s). - Uninstall: Right click -> View -> Custom Actions. In the Custom Actions view, right click Uninstall-> Add Custom Action. Double click "Application Folder". Select CustomAction Primary Output. Click OK.
Now, to add the virtual directory input textbox interface:
In Solution Explorer, right click the Setup Project -> View -> User Interface. Right click Install Start section -> Add dialog. Select Textboxes (A), click OK. Drag the new dialog just before the Welcome dialog. Right click new dialog -> Properties window. Set the following properties (without the ""s): BannerText
to "Virtual Directory Name", BodyText
to "Specify the Virtual Directory Name", Edit1Label
to "Virtual Dir:", Edit1Property
to "VIRTDIR", Edit1Value
to "WebToInstall", or specify your predefined virtual directory name. Set Edit2Visible
, Edit3Visible
, and Edit4Visible
to "False".
Points of Interest
That's it! This code also shows how to keep track of installer data, using the installer state. And that sometimes when we get stuck, trying to do things only using one way, when there is a more easy one, which can be seen if we only just look things from the other way around.
History
- Nov. 16, 2006 - Initial version.