/ Published in: C#
URL: create-web-site-iis6
Expand |
Embed | Plain Text
/// <summary> /// Creates new ASP.NET website on IIS /// </summary> /// <param name="siteName"></param> /// <returns></returns> [WebMethod] public string CreateWebsite(string siteName) { try { //Initialize configuration variables string metabasePath = Convert.ToString(ConfigurationManager.AppSettings["metabasePath"]); string frameworkVersion = Convert.ToString(ConfigurationManager.AppSettings["frameworkVersion"].ToString()); // al parecer de aqui agarra el sitio string physicalPath = Convert.ToString(ConfigurationManager.AppSettings["defaultFileLocation"].ToString()); string defaultAppPool = ConfigurationManager.AppSettings["defaultAppPool"].ToString();//Host Header Info string hostHeader = siteName.Replace("www.", string.Empty).Replace(".com", string.Empty); hosts[0] = ":80:" + hostHeader + ".com"; hosts[1] = ":80:" + "www." + hostHeader + ".com"; //Gets unique site id for the new website int siteId = GetUniqueSiteId(metabasePath); //Extracts the directory entry string className = objDirEntry.SchemaClassName; if (!className.EndsWith("Service")) return "Invalid configuration variables"; //creates new website by specifying site name and host header DirectoryEntry newSite = objDirEntry.Children.Add(Convert.ToString(siteId), (className.Replace("Service", "Server"))); newSite.Properties["ServerComment"][0] = siteName; newSite.Properties["ServerBindings"].Value = hosts; newSite.Invoke("Put", "ServerAutoStart", 1); newSite.Invoke("Put", "ServerSize", 1); newSite.CommitChanges(); //Creates root directory by specifying the local path, default document and permissions DirectoryEntry newSiteVDir = newSite.Children.Add("Root", "IIsWebVirtualDir"); newSiteVDir.Properties["Path"][0] = physicalPath; newSiteVDir.Properties["EnableDefaultDoc"][0] = true; newSiteVDir.Properties["DefaultDoc"].Value = "default.aspx"; newSiteVDir.Properties["AppIsolated"][0] = 2; newSiteVDir.Properties["AccessRead"][0] = true; newSiteVDir.Properties["AccessWrite"][0] = false; newSiteVDir.Properties["AccessScript"][0] = true; newSiteVDir.Properties["AccessFlags"].Value = 513; newSiteVDir.Properties["AppRoot"][0] = @"/LM/W3SVC/" + Convert.ToString(siteId) + "/Root"; newSiteVDir.Properties["AppPoolId"].Value = defaultAppPool; newSiteVDir.Properties["AuthNTLM"][0] = true; newSiteVDir.Properties["AuthAnonymous"][0] = true; newSiteVDir.CommitChanges(); //Sets the framework version to 2.0 for the new website //Assuming the version will be something like n.n.nnnnn PropertyValueCollection lstScriptMaps = newSiteVDir.Properties["ScriptMaps"]; foreach (string scriptMap in lstScriptMaps) { if (scriptMap.Contains("Framework")) { arrScriptMaps.Add(versionRegex.Replace(scriptMap, frameworkVersion)); } else { arrScriptMaps.Add(scriptMap); } } newSiteVDir.Properties["ScriptMaps"].Value = arrScriptMaps.ToArray(); newSiteVDir.CommitChanges(); return "Website created successfully."; } catch (Exception ex) { return "Website creation failed. <br/>" + ex.Message; } } /// <summary> /// Creates new ASP.NET website on IIS /// </summary> /// <param name="siteName"></param> /// <param name="pysicalPath"></param> /// <returns></returns> [WebMethod] public string CreateWebsite2(string siteName, string physicalPath) { try { //Initialize configuration variables string metabasePath = Convert.ToString(ConfigurationManager.AppSettings["metabasePath"]); string frameworkVersion = Convert.ToString(ConfigurationManager.AppSettings["frameworkVersion"].ToString()); string defaultAppPool = ConfigurationManager.AppSettings["defaultAppPool"].ToString();//Host Header Info string hostHeader = siteName.Replace("www.", string.Empty).Replace(".com", string.Empty); hosts[0] = ":80:" + hostHeader + ".com"; hosts[1] = ":80:" + "www." + hostHeader + ".com"; //Gets unique site id for the new website int siteId = GetUniqueSiteId(metabasePath); //Extracts the directory entry string className = objDirEntry.SchemaClassName; if (!className.EndsWith("Service")) return "Invalid configuration variables"; //creates new website by specifying site name and host header DirectoryEntry newSite = objDirEntry.Children.Add(Convert.ToString(siteId), (className.Replace("Service", "Server"))); newSite.Properties["ServerComment"][0] = siteName; newSite.Properties["ServerBindings"].Value = hosts; newSite.Invoke("Put", "ServerAutoStart", 1); newSite.Invoke("Put", "ServerSize", 1); newSite.CommitChanges(); //Creates root directory by specifying the local path, default document and permissions DirectoryEntry newSiteVDir = newSite.Children.Add("Root", "IIsWebVirtualDir"); newSiteVDir.Properties["Path"][0] = physicalPath; newSiteVDir.Properties["EnableDefaultDoc"][0] = true; newSiteVDir.Properties["DefaultDoc"].Value = "default.aspx"; newSiteVDir.Properties["AppIsolated"][0] = 2; newSiteVDir.Properties["AccessRead"][0] = true; newSiteVDir.Properties["AccessWrite"][0] = false; newSiteVDir.Properties["AccessScript"][0] = true; newSiteVDir.Properties["AccessFlags"].Value = 513; newSiteVDir.Properties["AppRoot"][0] = @"/LM/W3SVC/" + Convert.ToString(siteId) + "/Root"; newSiteVDir.Properties["AppPoolId"].Value = defaultAppPool; newSiteVDir.Properties["AuthNTLM"][0] = true; newSiteVDir.Properties["AuthAnonymous"][0] = true; newSiteVDir.CommitChanges(); //Sets the framework version to 2.0 for the new website //Assuming the version will be something like n.n.nnnnn PropertyValueCollection lstScriptMaps = newSiteVDir.Properties["ScriptMaps"]; foreach (string scriptMap in lstScriptMaps) { if (scriptMap.Contains("Framework")) { arrScriptMaps.Add(versionRegex.Replace(scriptMap, frameworkVersion)); } else { arrScriptMaps.Add(scriptMap); } } newSiteVDir.Properties["ScriptMaps"].Value = arrScriptMaps.ToArray(); newSiteVDir.CommitChanges(); return "Website created successfully."; } catch (Exception ex) { return "Website creation failed. <br/>" + ex.Message; } } /// <summary> /// Retunrs Unique SiteId for the new website /// </summary> /// <param name="metabasePath"></param> /// <returns></returns> private int GetUniqueSiteId(string metabasePath) { int siteId = 1; foreach (DirectoryEntry e in objDirEntry.Children) { if (e.SchemaClassName == "IIsWebServer") { int id = Convert.ToInt32(e.Name); if (id >= siteId) siteId = id + 1; } } return siteId; }
You need to login to post a comment.
