Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何以编程方式添加IIS处理程序映射_C#_Iis 7 - Fatal编程技术网

C# 如何以编程方式添加IIS处理程序映射

C# 如何以编程方式添加IIS处理程序映射,c#,iis-7,C#,Iis 7,我正在寻找使用Microsoft.Web.Administration.dll在IIS 7中添加处理程序映射的方法。是否有一种方法可用于ServerManager对象 如果通过GUI进行添加,以下是要遵循的步骤,但同样,我需要以编程方式完成此操作 这是我用来启用ISAPI限制的代码,处理程序映射是否有类似的功能 public override void AddIsapiAndCgiRestriction(string description, string path, bool isAllowe

我正在寻找使用Microsoft.Web.Administration.dll在IIS 7中添加处理程序映射的方法。是否有一种方法可用于ServerManager对象

如果通过GUI进行添加,以下是要遵循的步骤,但同样,我需要以编程方式完成此操作

这是我用来启用ISAPI限制的代码,处理程序映射是否有类似的功能

public override void AddIsapiAndCgiRestriction(string description, string path, bool isAllowed)
{
    using (ServerManager serverManager = new ServerManager())
    {
        Configuration config = serverManager.GetApplicationHostConfiguration();
        ConfigurationSection isapiCgiRestrictionSection = config.GetSection("system.webServer/security/isapiCgiRestriction");
        ConfigurationElementCollection isapiCgiRestrictionCollection = isapiCgiRestrictionSection.GetCollection();
        ConfigurationElement addElement = isapiCgiRestrictionCollection.CreateElement("add");
        addElement["path"] = path;
        addElement["allowed"] = isAllowed;
        addElement["description"] = description;
        isapiCgiRestrictionCollection.Add(addElement);
        serverManager.CommitChanges();
    }
}

这就是我最终使用的解决方案:

public void AddHandlerMapping(string siteName, string name, string executablePath)
{
    using (ServerManager serverManager = new ServerManager())
    {
        Configuration siteConfig = serverManager.GetApplicationHostConfiguration();
        ConfigurationSection handlersSection = siteConfig.GetSection("system.webServer/handlers", siteName);
        ConfigurationElementCollection handlersCollection = handlersSection.GetCollection();

        bool exists = handlersCollection.Any(configurationElement => configurationElement.Attributes["name"].Value.Equals(name));

        if (!exists)
        {
            ConfigurationElement addElement = handlersCollection.CreateElement("add");
            addElement["name"] = name;
            addElement["path"] = "*";
            addElement["verb"] = "*";
            addElement["modules"] = "IsapiModule";
            addElement["scriptProcessor"] = executablePath;
            addElement["resourceType"] = "Unspecified";
            addElement["requireAccess"] = "None";
            addElement["preCondition"] = "bitness32";
            handlersCollection.Add(addElement);
            serverManager.CommitChanges();
        }
    }
}