Posted By

ralarcon on 11/05/10


Tagged

web create in site IIS6 create-web-site-iis6


Versions (?)

create-web-site-iis6


 / Published in: C#
 

URL: create-web-site-iis6

  1. /// <summary>
  2. /// Creates new ASP.NET website on IIS
  3. /// </summary>
  4. /// <param name="siteName"></param>
  5. /// <returns></returns>
  6. [WebMethod]
  7. public string CreateWebsite(string siteName) {
  8. try {
  9. //Initialize configuration variables
  10. string metabasePath = Convert.ToString(ConfigurationManager.AppSettings["metabasePath"]);
  11. string frameworkVersion = Convert.ToString(ConfigurationManager.AppSettings["frameworkVersion"].ToString());
  12. // al parecer de aqui agarra el sitio
  13. string physicalPath = Convert.ToString(ConfigurationManager.AppSettings["defaultFileLocation"].ToString());
  14. string defaultAppPool = ConfigurationManager.AppSettings["defaultAppPool"].ToString();//Host Header Info
  15. object[] hosts = new object[2];
  16. string hostHeader = siteName.Replace("www.", string.Empty).Replace(".com", string.Empty);
  17. hosts[0] = ":80:" + hostHeader + ".com";
  18. hosts[1] = ":80:" + "www." + hostHeader + ".com";
  19.  
  20. //Gets unique site id for the new website
  21. int siteId = GetUniqueSiteId(metabasePath);
  22.  
  23. //Extracts the directory entry
  24. DirectoryEntry objDirEntry = new DirectoryEntry(metabasePath);
  25. string className = objDirEntry.SchemaClassName;
  26. if (!className.EndsWith("Service")) return "Invalid configuration variables";
  27.  
  28. //creates new website by specifying site name and host header
  29. DirectoryEntry newSite = objDirEntry.Children.Add(Convert.ToString(siteId), (className.Replace("Service", "Server")));
  30. newSite.Properties["ServerComment"][0] = siteName;
  31. newSite.Properties["ServerBindings"].Value = hosts;
  32. newSite.Invoke("Put", "ServerAutoStart", 1);
  33. newSite.Invoke("Put", "ServerSize", 1);
  34. newSite.CommitChanges();
  35.  
  36. //Creates root directory by specifying the local path, default document and permissions
  37. DirectoryEntry newSiteVDir = newSite.Children.Add("Root", "IIsWebVirtualDir");
  38. newSiteVDir.Properties["Path"][0] = physicalPath;
  39. newSiteVDir.Properties["EnableDefaultDoc"][0] = true;
  40. newSiteVDir.Properties["DefaultDoc"].Value = "default.aspx";
  41. newSiteVDir.Properties["AppIsolated"][0] = 2;
  42. newSiteVDir.Properties["AccessRead"][0] = true;
  43. newSiteVDir.Properties["AccessWrite"][0] = false;
  44. newSiteVDir.Properties["AccessScript"][0] = true;
  45. newSiteVDir.Properties["AccessFlags"].Value = 513;
  46. newSiteVDir.Properties["AppRoot"][0] = @"/LM/W3SVC/" + Convert.ToString(siteId) + "/Root";
  47. newSiteVDir.Properties["AppPoolId"].Value = defaultAppPool;
  48. newSiteVDir.Properties["AuthNTLM"][0] = true;
  49. newSiteVDir.Properties["AuthAnonymous"][0] = true;
  50. newSiteVDir.CommitChanges();
  51.  
  52. //Sets the framework version to 2.0 for the new website
  53. //Assuming the version will be something like n.n.nnnnn
  54. Regex versionRegex = new Regex(@"(?<=\\v)\d{1}\.\d{1}\.\d{1,5}(?=\\)");
  55. PropertyValueCollection lstScriptMaps = newSiteVDir.Properties["ScriptMaps"];
  56. System.Collections.ArrayList arrScriptMaps = new System.Collections.ArrayList();
  57. foreach (string scriptMap in lstScriptMaps) {
  58. if (scriptMap.Contains("Framework")) {
  59. arrScriptMaps.Add(versionRegex.Replace(scriptMap, frameworkVersion));
  60. } else {
  61. arrScriptMaps.Add(scriptMap);
  62. }
  63. }
  64. newSiteVDir.Properties["ScriptMaps"].Value = arrScriptMaps.ToArray();
  65. newSiteVDir.CommitChanges();
  66. return "Website created successfully.";
  67. }
  68. catch (Exception ex) {
  69. return "Website creation failed. <br/>" + ex.Message;
  70. }
  71. }
  72.  
  73. /// <summary>
  74. /// Creates new ASP.NET website on IIS
  75. /// </summary>
  76. /// <param name="siteName"></param>
  77. /// <param name="pysicalPath"></param>
  78. /// <returns></returns>
  79. [WebMethod]
  80. public string CreateWebsite2(string siteName, string physicalPath) {
  81. try {
  82. //Initialize configuration variables
  83. string metabasePath = Convert.ToString(ConfigurationManager.AppSettings["metabasePath"]);
  84. string frameworkVersion = Convert.ToString(ConfigurationManager.AppSettings["frameworkVersion"].ToString());
  85. string defaultAppPool = ConfigurationManager.AppSettings["defaultAppPool"].ToString();//Host Header Info
  86. object[] hosts = new object[2];
  87. string hostHeader = siteName.Replace("www.", string.Empty).Replace(".com", string.Empty);
  88. hosts[0] = ":80:" + hostHeader + ".com";
  89. hosts[1] = ":80:" + "www." + hostHeader + ".com";
  90.  
  91. //Gets unique site id for the new website
  92. int siteId = GetUniqueSiteId(metabasePath);
  93.  
  94. //Extracts the directory entry
  95. DirectoryEntry objDirEntry = new DirectoryEntry(metabasePath);
  96. string className = objDirEntry.SchemaClassName;
  97. if (!className.EndsWith("Service")) return "Invalid configuration variables";
  98.  
  99. //creates new website by specifying site name and host header
  100. DirectoryEntry newSite = objDirEntry.Children.Add(Convert.ToString(siteId), (className.Replace("Service", "Server")));
  101. newSite.Properties["ServerComment"][0] = siteName;
  102. newSite.Properties["ServerBindings"].Value = hosts;
  103. newSite.Invoke("Put", "ServerAutoStart", 1);
  104. newSite.Invoke("Put", "ServerSize", 1);
  105. newSite.CommitChanges();
  106.  
  107. //Creates root directory by specifying the local path, default document and permissions
  108. DirectoryEntry newSiteVDir = newSite.Children.Add("Root", "IIsWebVirtualDir");
  109. newSiteVDir.Properties["Path"][0] = physicalPath;
  110. newSiteVDir.Properties["EnableDefaultDoc"][0] = true;
  111. newSiteVDir.Properties["DefaultDoc"].Value = "default.aspx";
  112. newSiteVDir.Properties["AppIsolated"][0] = 2;
  113. newSiteVDir.Properties["AccessRead"][0] = true;
  114. newSiteVDir.Properties["AccessWrite"][0] = false;
  115. newSiteVDir.Properties["AccessScript"][0] = true;
  116. newSiteVDir.Properties["AccessFlags"].Value = 513;
  117. newSiteVDir.Properties["AppRoot"][0] = @"/LM/W3SVC/" + Convert.ToString(siteId) + "/Root";
  118. newSiteVDir.Properties["AppPoolId"].Value = defaultAppPool;
  119. newSiteVDir.Properties["AuthNTLM"][0] = true;
  120. newSiteVDir.Properties["AuthAnonymous"][0] = true;
  121. newSiteVDir.CommitChanges();
  122.  
  123. //Sets the framework version to 2.0 for the new website
  124. //Assuming the version will be something like n.n.nnnnn
  125. Regex versionRegex = new Regex(@"(?<=\\v)\d{1}\.\d{1}\.\d{1,5}(?=\\)");
  126. PropertyValueCollection lstScriptMaps = newSiteVDir.Properties["ScriptMaps"];
  127. System.Collections.ArrayList arrScriptMaps = new System.Collections.ArrayList();
  128. foreach (string scriptMap in lstScriptMaps) {
  129. if (scriptMap.Contains("Framework")) {
  130. arrScriptMaps.Add(versionRegex.Replace(scriptMap, frameworkVersion));
  131. } else {
  132. arrScriptMaps.Add(scriptMap);
  133. }
  134. }
  135. newSiteVDir.Properties["ScriptMaps"].Value = arrScriptMaps.ToArray();
  136. newSiteVDir.CommitChanges();
  137. return "Website created successfully.";
  138. }
  139. catch (Exception ex) {
  140. return "Website creation failed. <br/>" + ex.Message;
  141. }
  142. }
  143.  
  144. /// <summary>
  145. /// Retunrs Unique SiteId for the new website
  146. /// </summary>
  147. /// <param name="metabasePath"></param>
  148. /// <returns></returns>
  149. private int GetUniqueSiteId(string metabasePath) {
  150. int siteId = 1;
  151. DirectoryEntry objDirEntry = new DirectoryEntry(metabasePath);
  152. foreach (DirectoryEntry e in objDirEntry.Children) {
  153. if (e.SchemaClassName == "IIsWebServer") {
  154. int id = Convert.ToInt32(e.Name);
  155. if (id >= siteId)
  156. siteId = id + 1;
  157. }
  158. }
  159. return siteId;
  160. }

Report this snippet  

You need to login to post a comment.