4

我的 IIS 服务器有问题,

如何使用 C# 语言修改 ISAPI 元素?

例如:ASP.net V4.0 限制为“不允许”。我想设置为“允许”,如下图所示。

在此处输入图像描述

我可以使用此代码添加元素。但我无法修改。

using System;
using System.Text;
using Microsoft.Web.Administration;

internal static class Sample
{
   private static void Main()
   {
      using (ServerManager serverManager = new ServerManager())
      {
         Configuration config = serverManager.GetApplicationHostConfiguration();
         ConfigurationSection isapiFiltersSection = config.GetSection("system.webServer/isapiFilters");
         ConfigurationElementCollection isapiFiltersCollection = isapiFiltersSection.GetCollection();

         ConfigurationElement filterElement = isapiFiltersCollection.CreateElement("filter");
         filterElement["name"] = @"SalesQueryIsapi";
         filterElement["path"] = @"c:\Inetpub\www.contoso.com\filters\SalesQueryIsapi.dll";
         filterElement["enabled"] = true;
         filterElement["enableCache"] = true;
         isapiFiltersCollection.Add(filterElement);

         serverManager.CommitChanges();
      }
   }
}

谢谢你的建议。

4

2 回答 2

4

我找到了解决方案。我更改了如下代码。它奏效了。

private void buttonOK_Click(object sender, EventArgs e)
   {

        using (ServerManager serverManager = new ServerManager())
        {
            Configuration config = serverManager.GetApplicationHostConfiguration();
            ConfigurationSection isapiCgiRestrictionSection = config.GetSection("system.webServer/security/isapiCgiRestriction");
            ConfigurationElementCollection isapiCgiRestrictionCollection = isapiCgiRestrictionSection.GetCollection();
            foreach (ConfigurationElement element in isapiCgiRestrictionCollection)
            {
                element.SetAttributeValue("allowed", false);
            }

            ConfigurationElement addElement = isapiCgiRestrictionCollection.CreateElement("add");

            serverManager.CommitChanges();          
        }
    }

在此处输入图像描述

于 2013-03-11T15:24:09.397 回答
2

如果要添加限制,可以使用以下代码:

public static void AddIsapiRestriction(string name, string path)
{
    using (var serverManager = new ServerManager())
    {
        var config = serverManager.GetApplicationHostConfiguration();
        var isapiCgiRestrictionSection = config.GetSection("system.webServer/security/isapiCgiRestriction");
        var isapiCgiRestrictionCollection = isapiCgiRestrictionSection.GetCollection();
        if (isapiCgiRestrictionCollection.ToList().Exists(x => x.GetAttribute("path").Value.ToString() == path))
            return;
        var addElement = isapiCgiRestrictionCollection.CreateElement("add");
        addElement["description"] = name;
        addElement["path"] = path;
        addElement["allowed"] = true;
        isapiCgiRestrictionCollection.Add(addElement);
        serverManager.CommitChanges();              
    }            
}
于 2013-11-08T17:18:31.213 回答