2011-02-08 81 views
2

我希望能够以编程方式确定System.webServer/Security/requestFiltering部分是否存在于我的应用程序的web.config文件中。 我能够使用下面的代码为system.web等其他部分做到这一点,但到目前为止没有与system.WebServer的运气。如何检查System.webServer/Security/requestFiltering部分是否以编程方式存在?

var config = WebConfigurationManager.OpenWebConfiguration("~"); 

    HttpRuntimeSection section = config.GetSection("system.web/httpRuntime") as HttpRuntimeSection; 

Label1.Text = section.MaxRequestLength.ToString(); 

回答

2

为什么不像任何XML文件一样阅读web.config并查找节点?你可以这样做:

XmlDocument xmlDoc = new XmlDocument(); 
xmlDoc.Load(Server.MapPath("~/Web.config")); 

XmlNode n = xmlDoc.SelectSingleNode("/configuration/System.webServer/Security/requestFiltering"); 

if (n != null) 
{ 
    // Do whatever you want... 
} 
相关问题