2011-02-08 80 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... 
} 
相關問題