0

I am trying to create a Virtual Directory progeammatically using the below code:

ServerManager iisManager = new ServerManager();
iisManager.Sites.Add("NewSite", "http", "*:8080:", @"D:\mine\TestApps\TestAppXML");
iisManager.CommitChanges();

this code is working fine when using through Console application. but when I am using the same code through Web Application, it is not doing the desired job. Nor does it give any error. Anyone has ideas about why the same code is not working with Web Application?

rakaur
  • 1
  • 1
  • I guess user which is used by your Web Application does not have access to that stuff. – MarcinJuraszek Dec 02 '13 at 03:14
  • 1
    I am running the Web Application as Administrator. but the console application is even running without the administrator. – rakaur Dec 02 '13 at 03:31
  • Let's be clear about the user we are talking about. What user is the `Identity` of the Application Pool set to? – John Koerner Dec 02 '13 at 22:25
  • I am not setting anything explicitly. I am running with the default Application Pool and when opening the VS, running it as administrator. just using the above 3 lines of code and adding a reference to Microsoft.Web.Administration dll. Not changing any other properties. – rakaur Dec 03 '13 at 01:03

1 Answers1

0

I dont see were you are grabing the website id.

The way to do this is to manipulate the Site.Applications collection which is a flattened tree of all the applications in your site.

If you would like to create a virtual directory try this:

//Get site information
Site site = srvam.Sites.First(s => s.Id == 3);
if (!SiteExists(srvman, siteId))
                    throw new ApplicationException();
Application iisManager = site.Applications.First(a => a.Path == "/");
iisManager.VirtualDirectories.Add("/vdir_1", @"D:\MySite\other_content");
srvam.CommitChanges();

If you would like to create a virtual Application use this code:

//Get site information
    Site site = srvman.Sites.First(s => s.Id == 3);
if (!SiteExists(srvman, siteId))
                    throw new ApplicationException();
Application app = 
   site.Applications.Add("/app_2", @"d:\mysite\other_content");
app.ApplicationPoolName = "MySite";
srvman.CommitChanges();

There is a great write up here

Community
  • 1
  • 1
pool pro
  • 2,084
  • 12
  • 21