在我的 Web.Config 檔案中,我有以下內容:
<system.webServer>
<handlers>
<add name="HANDLERNAME" verb="*" path="PATH.axd" type="HANDLERTYPE">
</handlers>
</system.webServer>
在我運行一段特定的代碼之前,我想檢查處理程式是否存在于我的 Web.Config 檔案中。
這是我能做到的嗎?
我試過ConfigurationManager.GetSection("system.webServer/handlers")了:沒有成功,因為它回傳null。
任何幫助將不勝感激!
uj5u.com熱心網友回復:
我找到了兩種方法來檢查 web.config 中的處理程式
XmlDocument doc = new XmlDocument();
doc.Load(path); *//path is the location of the web.config file*
XmlElement root = doc.DocumentElement;
XmlNode nodes = root.SelectSingleNode("/system.webServer");
XmlNodeList childnotes = nodes.ChildNodes;
bool isExist = false;;
foreach (XmlNode node in childnotes)
{
if (node.Name.Contains("handlers"))
{
isExist = node.OuterXml.Contains("HANDLERNAME");
}
}
你可以檢查isExist的值
另一種方法是將整個 web.config 作為字串獲取并檢查它是否包含HANDLERNAME
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/468071.html
上一篇:阻止專案添加到字典
