Quantcast
Channel: All Forums
Viewing all articles
Browse latest Browse all 26542

Create the website dynamically(programatic way)

$
0
0

Dear Support,

Based on our requirement we are created the virtual directory,pools in iis 6 and map the pool to the virtual directory,set the frame work to the virtual directory, in programmatic way using c# , but we i browse the site it shows an  invalid data or resource not avaliblie and etc.

but when create the virtual directory in programmatic way and set this virtual directory to the application pool, frame work in manually it working fine.

when i create the pool and assign the pool to virtual directory,set the frame work to virtual directory in programmatic way is not working.please suggest me.

i am using ii6, and in web.config i am implemented the impersonate also.

Thanks and best Regards,

Vempalli Bavaji



  i am using the below code and need to give the permission of the folders,xml files and etc.

  public  static void CreateVirtualDir(string WebSite, string AppName, string Path)
  {

      System.DirectoryServices.DirectoryEntry IISSchema = new System.DirectoryServices.DirectoryEntry("IIS://" + WebSite + "/Schema/AppIsolated");
      bool CanCreate = !(IISSchema.Properties["Syntax"].Value.ToString().ToUpper() == "BOOLEAN");
      IISSchema.Dispose();

      if (CanCreate)
      {
          bool PathCreated = false;

          //try
         // {
              System.DirectoryServices.DirectoryEntry IISAdmin = new System.DirectoryServices.DirectoryEntry("IIS://" + WebSite + "/W3SVC/1/Root");

              ////make sure folder exists
              //if (!System.IO.Directory.Exists(Path))
              //{
              //    System.IO.Directory.CreateDirectory(Path);
              //    PathCreated = true;
              //}

              //If the virtual directory already exists then delete it
              //foreach (System.DirectoryServices.DirectoryEntry VD in IISAdmin.Children)
              //{
              //    if (VD.Name == AppName)
              //    {
              //        IISAdmin.Invoke("Delete", new string[] {
              //          VD.SchemaClassName,
              //          AppName
              //      });
              //        IISAdmin.CommitChanges();
              //        break; // TODO: might not be correct. Was : Exit For
              //    }
              //}

              //Create and setup new virtual directory
              System.DirectoryServices.DirectoryEntry VDir = IISAdmin.Children.Add(AppName, "IIsWebVirtualDir");
              VDir.Properties["Path"][0] = Path;
              VDir.Properties["AppFriendlyName"][0] = AppName;
              VDir.Properties["EnableDirBrowsing"][0] = false;
              VDir.Properties["AccessRead"][0] = true;
              VDir.Properties["AccessExecute"][0] = true;
              VDir.Properties["AccessWrite"][0] = false;
              VDir.Properties["AccessScript"][0] = true;
              VDir.Properties["AuthNTLM"][0] = true;
              VDir.Properties["EnableDefaultDoc"][0] = true;
              VDir.Properties["DefaultDoc"][0] = "default.htm,default.aspx,default.asp";
              VDir.Properties["AspEnableParentPaths"][0] = true;
              VDir.CommitChanges();

              //the following are acceptable params
              //INPROC = 0
              //OUTPROC = 1
              //POOLED = 2
              VDir.Invoke("AppCreate", 1);



             
            











         // }
          //catch (Exception Ex)
          //{
          //    if (PathCreated)
          //    {
          //        System.IO.Directory.Delete(Path);
          //    }
          //    throw Ex;
          //}
      }
  }

 public static void CreateAppPool(string metabasePath, string appPoolName)
  {
      //  metabasePath is of the form "IIS://<servername>/W3SVC/AppPools"
      //    for example "IIS://localhost/W3SVC/AppPools"
      //  appPoolName is of the form "<name>", for example, "MyAppPool"
      //Console.WriteLine("\nCreating application pool named {0}/{1}:", metabasePath, appPoolName);

      try
      {
          if (metabasePath.EndsWith("/W3SVC/AppPools"))
          {
              DirectoryEntry newpool;
              DirectoryEntry apppools = new DirectoryEntry(metabasePath);
              newpool = apppools.Children.Add(appPoolName, "IIsApplicationPool");
             // newpool.Properties["managedRuntimeVersion"].Value = "v4.0";
             
              newpool.CommitChanges();
              apppools.CommitChanges();
              Console.WriteLine(" Done.");
          }
          else
              Console.WriteLine(" Failed in CreateAppPool; application pools can only be created in the */W3SVC/AppPools node.");
      }
      catch (Exception ex)
      {
          Console.WriteLine("Failed in CreateAppPool with the following exception: \n{0}", ex.Message);
      }

      

  }

 public static void AssignVDirToAppPool(string metabasePath, string appPoolName)
 {
     //  metabasePath is of the form "IIS://<servername>/W3SVC/<siteID>/Root[/<vDir>]"
     //    for example "IIS://localhost/W3SVC/1/Root/MyVDir"
     //  appPoolName is of the form "<name>", for example, "MyAppPool"
     Console.WriteLine("\nAssigning application {0} to the application pool named {1}:", metabasePath, appPoolName);

     try
     {
         DirectoryEntry vDir = new DirectoryEntry(metabasePath);
         string className = vDir.SchemaClassName.ToString();
         if (className.EndsWith("VirtualDir"))
         {
             object[] param = { 0, appPoolName, true };
             vDir.Invoke("AppCreate3", param);
            //vDir.Properties["AppIsolated"][0] = "2";

            // vDir.Properties["AppIsolated"][1] = "4";
             vDir.Properties["ScriptMaps"].Value = "4.0.30319";
            vDir.CommitChanges();
            Console.WriteLine(" Done.");

            //System.DirectoryServices.DirectoryEntry mine = new System.DirectoryServices.DirectoryEntry();
            //mine.Path = "IIS://LocalHost/w3svc/1/Root";

            //mine.Properties["ScriptMaps"].Add(@".gif,c:\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG");

            //mine.CommitChanges();

            //string frameworkVersion = Convert.ToString(ConfigurationManager.AppSettings["frameworkVersion"].ToString());
            //Regex versionRegex = new Regex(@"(?<=\\v)\d{1}\.\d{1}\.\d{1,5}(?=\\)");
            //PropertyValueCollection lstScriptMaps = vDir.Properties["ScriptMaps"];
            //System.Collections.ArrayList arrScriptMaps = new System.Collections.ArrayList();
            //foreach (string scriptMap in lstScriptMaps)
            //{
            //    if (scriptMap.Contains("Framework"))
            //    {
            //        arrScriptMaps.Add(versionRegex.Replace(scriptMap, frameworkVersion));
                    
            //    }
            //    else
            //    {
            //        arrScriptMaps.Add(scriptMap);
            //    }
            //}
            //vDir.Properties["ScriptMaps"].Value = arrScriptMaps.ToArray();
            //vDir.CommitChanges();
            ////return "Website created successfully.";










         }
         else
             Console.WriteLine(" Failed in AssignVDirToAppPool; only virtual directories can be assigned to application pools");
     }
     catch (Exception ex)
     {
         Console.WriteLine("Failed in AssignVDirToAppPool with the following exception: \n{0}", ex.Message);
     }
 }


Viewing all articles
Browse latest Browse all 26542

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>