繁体   English   中英

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

[英]Not able to create dynamic web application in IIS7 and above

成功调用以下方法后,无法在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中查看应用程序/虚拟目录。

我已经尝试过 ,但是没有成功。

您可以通过以下代码在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;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM