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

问题描述 投票:0回答:2

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

这些是通过 GUI 添加时要遵循的步骤,但同样,我需要以编程方式完成此操作。 http://coderock.net/how-to-create-a-handler-mapping-for-an-asp-net-iis-7-with-application-running-in-integrated-mode/

这是我用来启用 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();
    }
}
c# iis-7
2个回答
9
投票

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

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

0
投票

我使用你的代码并将其配置为在 C# 中为 php 添加 FastCguModule 以使用 WIX 工具。重要的是你将提交放在配置文件的第一位:

        [CustomAction]
    public static ActionResult AddHandlerMapping(Session session)
    {
        string siteName = "Default Web Site";
        string name = "PHP";
        string executablePath = "C:\\PHP\\php-cgi.exe";

        using (ServerManager serverManager = new ServerManager())
         {
             Microsoft.Web.Administration.Configuration siteConfig = serverManager.GetApplicationHostConfiguration();

             Microsoft.Web.Administration.ConfigurationSection handlersSection = siteConfig.GetSection("system.webServer/handlers", siteName);
             Microsoft.Web.Administration.ConfigurationElementCollection handlersCollection = handlersSection.GetCollection();

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

             if (!exists)
             {
                 Microsoft.Web.Administration.ConfigurationElement addElement = handlersCollection.CreateElement("add");
                 addElement["name"] = name;
                 addElement["path"] = "*.php";
                 addElement["verb"] = "*";
                 addElement["modules"] = "FastCgiModule";
                 addElement["scriptProcessor"] = executablePath;
                 addElement["resourceType"] = "File";
                 //addElement["requireAccess"] = "Script";
                 //addElement["preCondition"] = "bitness64";
                handlersCollection.AddAt(0, addElement);
                 serverManager.CommitChanges();
                 return ActionResult.Success;
             }
         }
        return ActionResult.Success;
    }
© www.soinside.com 2019 - 2024. All rights reserved.