Click here to Skip to main content
Click here to Skip to main content

Allow user to set physical directory path during Web Project deployment

, 16 Nov 2006
Rate this:
Please Sign up or sign in to vote.
How to deploy a Web application to a user defined physical directory.

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)
    ' Execute inherited Install
    MyBase.Install(stateSaver)

    ' Get Assembly
    Dim Asm As System.Reflection.Assembly = _
        System.Reflection.Assembly.GetExecutingAssembly

    Dim DirLocation As String = Path.GetDirectoryName(Asm.Location)

    REM *******************************************************
    REM Create IIS Virtual Dir 

    ' TODO: Search for the Server Number <-------¬
    Dim vRoot = GetObject("IIS://LocalHost/W3svc/1/Root")

    ' Create de Virtual Dir
    Dim vDir = vRoot.Create("IIsWebVirtualDir", _
               Me.Context.Parameters.Item("dir"))

    ' Set the Physical Path for the virtual Dir
    vDir.Path = DirLocation

    ' Set the default Doc
    vDir.DefaultDoc = "Default.aspx"

    ' Actually set Info
    vDir.SetInfo()

    'Create an application in-process. 
    vDir.AppCreate(True)

    ' Save the Virtual Dir
    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:

' Override 'Uninstall' method of Installer class.
Public Overrides Sub Uninstall(ByVal mySavedState As IDictionary)
    MyBase.Uninstall(mySavedState)
    Try
        ' Get virtual dir
        Dim vRoot = GetObject("IIS://LocalHost/W3svc/1/Root")

        ' Delete Virtual Dir
        vRoot.Delete("IIsWebVirtualDir", mySavedState("VirtualDir"))

    Catch ex As Exception

    End Try
End Sub 'Uninstall

And this is the method called after all the setup deployment steps are executed, it opens the Web application default page in Internet Explorer:

' Override the 'Commit' method of the Installer class.
Public Overrides Sub Commit(ByVal mySavedState As IDictionary)
    MyBase.Commit(mySavedState)
    Try
    ' Open the virtual dir after install
        Dim P As New Process
        P.Start("iexplore.exe", "http://localhost/" & _
                Me.Context.Parameters.Item("dir") & "/")
    Catch ex As Exception

    End Try
End Sub 'Commit

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

rmortega77
Software Developer CIMEX S.A.
Cuba Cuba
Rodolfo Ortega is a Cuban Computer Scientist. He works as IT Auditor for the CIMEX S.A. subsidiary in Holguin, Cuba. He lives and works in Holguin, in the eastern part of the island of Cuba.

You can contact him at rodolfom[]cimex.com.cu for any personal message: Ideas on new articles, bibliography about new APIs, questions, are wellcome.

Submit questions related with current article to the article forum.

Comments and Discussions

 
Generalim installing a wcf service Pinmembersfsddf11-Sep-12 20:46 
GeneralGreat Article! One question... Pinmemberma9169-Nov-10 3:14 
GeneralMy vote of 4 PinmemberBalaji_Bala26-Jul-10 22:52 
GeneralPhysical Path [modified] PinmemberShakeel Ahmed Awan24-Sep-08 2:37 
Generalalternative Pinmembermoin0215-Dec-06 5:24 
GeneralRe: alternative Pinmemberjeff reece6-Dec-06 15:03 
GeneralRe: alternative Pinmembermoin0217-Dec-06 16:40 
GeneralRe: alternative Pinmemberrmortega777-Dec-06 9:00 
QuestionWhat is a server number? PinmemberCarmela Pham29-Nov-06 9:49 
AnswerRe: What is a server number? Pinmemberrmortega7730-Nov-06 5:39 
GeneralRe: What is a server number? PinmemberCarmela Pham30-Nov-06 8:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140709.1 | Last Updated 16 Nov 2006
Article Copyright 2006 by rmortega77
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid