我的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();
}
}
}
谢谢你的建议。
答案 0 :(得分: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();
}
}
答案 1 :(得分: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();
}
}