无法在IIS7及更高版本中创建动态Web应用程序

时间:2014-06-13 10:46:35

标签: c# iis-7 virtual-directory

成功调用以下方法后无法在IIS中查看应用程序:

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();

我不知道如何通过c#代码动态创建子域。我们只是试图在上面实现。但无法在iis中查看应用程序/虚拟目录。

我尝试了this,但没有成功。

1 个答案:

答案 0 :(得分:3)

您可以通过以下代码在IIS中创建动态Web应用程序并确保您的Visual Studio在管理员模式下打开,因为我在IIS中创建WebApplication时发现访问被拒绝问题

- Web配置文件

<!---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#代码

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;
    }