Not able to view application in IIS after successfully calling below method:
ServerManager serverMgr = new ServerManager();
Configuration config = serverMgr.GetApplicationHostConfiguration();
ConfigurationSection isapiCgiRestrictionSection = config.GetSection("system.webServer/security/isapiCgiRestriction");
ConfigurationElementCollection isapiCgiRestrictionCollection = isapiCgiRestrictionSection.GetCollection();
//ConfigurationElement addElement = isapiCgiRestrictionCollection.CreateElement("add");
//addElement["path"] = @"C:\Inetpub\wwwroot\";
//addElement["allowed"] = true;
//addElement["groupId"] = @"ContosoGroup";
//addElement["description"] = @"Contoso Extension";
//isapiCgiRestrictionCollection.Add(addElement);
//serverMgr.CommitChanges();
Site defaultSite = serverMgr.Sites["PharmaConnect"];
defaultSite.Applications.Add("/blogs3", @"C:\inetpub\wwwroot\blogs1");
serverMgr.CommitChanges();
I don't know how to create dynamically sub domain though c# code. We just tried to implement above. But unable to view application/virtual directory in iis.
I have tried this, but didn't get success.
You can create Dynamic Web Application in IIS by Following Code and Make sure that Your Visual Studio is Open in Administrator Mode because i was found Access Denied Issue while Creating WebApplication in IIS
-- Web Config file
<!---Default format is IIS://<your server name>/W3SVC-->
<add key="metabasePath" value="IIS://JAYDEV/W3SVC"/>
<!--Framework version of newly created website-->
<add key="frameworkVersion" value="4.0"/>
<!---Local path of newly created website -->
<add key="defaultFileLocation" value="C:\inetpub\wwwroot\Dynamic1"/>
<!---Application Pool of newly created website-->
<add key="defaultAppPool" value="DynamicPool"/>
-- C# Code
public ActionResult DynamicWeb()
{
try
{
string metabasePath = Convert.ToString(ConfigurationManager.AppSettings["metabasePath"]);
string frameworkVersion = Convert.ToString(ConfigurationManager.AppSettings["frameworkVersion"].ToString());
string physicalPath = Convert.ToString(ConfigurationManager.AppSettings["defaultFileLocation"].ToString());
string defaultAppPool = ConfigurationManager.AppSettings["defaultAppPool"].ToString();//Host Header Info
object[] hosts = new object[2];
string hostHeader = "Dynamic1"; //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
DirectoryEntry objDirEntry = new DirectoryEntry(metabasePath);
string className = objDirEntry.SchemaClassName;
//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] = "Dynamic1";
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();
PropertyValueCollection lstScriptMaps = newSiteVDir.Properties["ScriptMaps"];
System.Collections.ArrayList arrScriptMaps = new System.Collections.ArrayList();
foreach (string scriptMap in lstScriptMaps)
{
arrScriptMaps.Add(scriptMap);
}
newSiteVDir.Properties["ScriptMaps"].Value = arrScriptMaps.ToArray();
newSiteVDir.CommitChanges();
}
catch (Exception ex)
{
throw ex;
}
return View("Index");
}
private int GetUniqueSiteId(string metabasePath)
{
int siteId = 1;
DirectoryEntry objDirEntry = new DirectoryEntry(metabasePath);
foreach (DirectoryEntry e in objDirEntry.Children)
{
if (e.SchemaClassName == "IIsWebServer")
{
int id = Convert.ToInt32(e.Name);
if (id >= siteId)
siteId = id + 1;
}
}
return siteId;
}