Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: IIS7 C# Visual-Studio
Hi,
i work with windows server 2008r2, visual studio 2010, and iis7. I googled out c# code to programatically create a website in iis, assign an application pool to it, and do the authorization settings and the appli9caiton pool settings. After i create a website and appllication pool through c# code i instantiate an visual studio installer(.msi file) that will install the applications under the newly created website.
 
THE PROBLEM: Once i finish everything and expand my newly created site i see my applications installed. when i click on them under the features tab of iis i dont see the ASP.NET tab. because of that the other tabs(iis & managemant) are slao not functional. and when i attempot to open any of the aspx pages of my applicaitons it shows http 503: service not available error. here is the code i have used.
 
your help would be greatly appreciated. Thanks in advance.
 
//creating a new folder
     string Folderpath = @"c:\inetpub\wwwroot\" + textBox3.Text;
     if (!Directory.Exists(Folderpath))
     {
     Directory.CreateDirectory(Folderpath);
     MessageBox.Show("New Folder created!", "  Message  ");
     }
     else
     {
     MessageBox.Show("The Folder already exist. Please enter a different name!", "  Alert!!! ");
     this.Hide();
     System.Windows.Forms.Application.Restart();
     }
 
    //Port number validation
    IPAddress ipAddress = Dns.GetHostEntry("localhost").AddressList[0];
    try
    {
    TcpListener tcpListener = new TcpListener(ipAddress, Convert.ToInt32(textBox2.Text));
    tcpListener.Start();
    MessageBox.Show("Port No. is available" , "Message");
    }
    Catch (SocketException ex)
    {
    MessageBox.Show(ex.Message, "Please enter a different port number");
    this.Hide();
    System.Windows.Forms.Application.Restart();
    }
            
            
    //creating site
    string sitepath = @"C:\inetpub\wwwroot\" + textBox3.Text;
    ServerManager serverManager = new ServerManager();
    if (Directory.Exists(sitepath))
    {
    try
    {
    Directory.CreateDirectory(sitepath);
 Site mySite = serverManager.Sites.Add(textBox1.Text, sitepath, Convert.ToInt32(textBox2.Text));
    mySite.ServerAutoStart = true;
    MessageBox.Show("site created" , "Message");
    }
    catch (Exception site)
    {
    MessageBox.Show(site.Message, "Site Name error");
    this.Hide();
    System.Windows.Forms.Application.Restart();
    }
 
    //authentication settings
    try
    {
    Configuration config = serverManager.GetApplicationHostConfiguration();
    ConfigurationSection anonymousAuthenticationSection = config.GetSection("system.webServer/security/authentication/anonymousAuthentication", textBox1.Text);
    anonymousAuthenticationSection["enabled"] = false;
    ConfigurationSection basicAuthenticationSection = config.GetSection("system.webServer/security/authentication/basicAuthentication", textBox1.Text);
    basicAuthenticationSection["enabled"] = false;
    ConfigurationSection windowsAuthenticationSection = config.GetSection("system.webServer/security/authentication/windowsAuthentication", textBox1.Text);
    windowsAuthenticationSection["enabled"] = true;
    }
    catch(Exception config)
    {
    MessageBox.Show(config.Message,"Configuration error");
    this.Hide();
    System.Windows.Forms.Application.Restart();
    }
 
                
    //creating application pool and its settings
    try
    {
    serverManager.ApplicationPools.Add(textBox4.Text);
    ApplicationPool apppool = serverManager.ApplicationPools[textBox4.Text];
    Site site = serverManager.Sites[textBox1.Text];
    serverManager.Sites[textBox1.Text].Applications[0].ApplicationPoolName = textBox4.Text;
    Configuration config = serverManager.GetWebConfiguration("Default Web Site"); 
    ConfigurationSection section = config.GetSection("system.webServer/defaultDocument"); 
    ConfigurationAttribute enabled = section.GetAttribute("enabled"); 
    enabled.Value = true;
                    
                    
    config = serverManager.GetApplicationHostConfiguration();
    section = config.GetSection("system.webServer/defaultDocument", textBox1.Text);
    enabled = section.GetAttribute("enabled");
    enabled.Value = false;
    apppool.ManagedPipelineMode = ManagedPipelineMode.Classic;
    apppool.ManagedRuntimeVersion = "V4.0";
    apppool.AutoStart = true;
                   
    }
    catch(Exception pool)
    {
    MessageBox.Show(pool.Message,"Applicationpool error");
    this.Hide();
    System.Windows.Forms.Application.Restart();
    }
 
    serverManager.CommitChanges();
    DialogResult dlg;
    dlg = MessageBox.Show("Website and its settings has been successfully configured. Please click on proceed", "Message" , MessageBoxButtons.OK);
     if (dlg == DialogResult.OK)
     {
     Proceed.Enabled = true;
     Next.Enabled = false;
     }
                
}
Posted 18-Apr-13 20:01pm
Edited 18-Apr-13 20:03pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 6,769
1 Sergey Alexandrovich Kryukov 6,338
2 DamithSL 5,408
3 Manas Bhardwaj 4,816
4 Maciej Los 4,195


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 19 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100