Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IIS web deploy - create virtual directories?

When developing a site on VS2010/IIS 7.5, I'm using Web Deploy to publish the site from my machine to the dev site server.

The site has about 40 virtual directories, and I'd like to automagically create those on the server during the deployment. Is there a simple way to do this?

I'm considering writing a small app that would load the list from a file or database and create them on demand. The directories have different physical paths on my development machine than on the web server, also, which throws a wrench into the works.

like image 647
3Dave Avatar asked Nov 19 '10 20:11

3Dave


2 Answers

If you're using MSBuild for your web deploy you can write a CustomBuildTask in .net that you can use to create your virtual directory.

There are a lot of resources on how to create and consume a custom build task, but here is the code I use to create a virtual directory using a custom build task:

public void CreateVirtualDirectory()
{

    DirectoryEntry oDE = new DirectoryEntry("IIS://" +
            this._strServerName + "/W3SVC/" + _webSiteID + "/Root");


    //Get Default Web Site
    DirectoryEntries oDC = oDE.Children;

    //Add row to schema
    DirectoryEntry oVirDir = oDC.Add(this._strVDirName,
                oDE.SchemaClassName.ToString());

    //Commit changes for Schema class File
    oVirDir.CommitChanges();


    //Set virtual directory to physical path
    oVirDir.Properties["Path"].Value = this._strPhysicalPath;

    //Set read access
    oVirDir.Properties["AccessRead"][0] = true;

    //Set the default docs
    oVirDir.Properties["EnableDefaultDoc"][0] = true;
    oVirDir.Properties["DefaultDoc"][0] = "default.aspx";

    //set the name
    oVirDir.Properties["AppFriendlyName"][0] = this._strVDirName;

    //do it
    oVirDir.Invoke("AppCreate", true);


    //set the application pool
    if (!string.IsNullOrEmpty(_strApplicationPool))
    {
        object[] param = { 0, _strApplicationPool, true };
        oVirDir.Invoke("AppCreate3", param);
        oVirDir.Properties["AppIsolated"][0] = "2";
    }

    //Save all the changes
    oVirDir.CommitChanges();
}
like image 132
AaronS Avatar answered Oct 07 '22 01:10

AaronS


I've not done any custom programming against WebDeploy, but I've seen reference that there is an API for it. I can't seem to find official documentation on it, but perhaps this blog+sample app could provide a start: Web Deploy API Web Application

like image 40
moribvndvs Avatar answered Oct 06 '22 23:10

moribvndvs