![](/img/trans.png)
[英]How can I access web.config Profile properties in ASP.NET 4.5?
[英]How can I access web.config from an ASP.NET Custom Control?
在我的 web.config 文件中,我有這樣的代碼:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
...
<section name="UninstallSurveySettings" type="dashboard.UninstallSurveyConfig" />
</configSections>
...
<UninstallSurveySettings>
<add key="fileLocation" value="C:\inetpub\wwwroot\output\" />
</UninstallSurveySettings>
...
</configuration>
我需要能夠從我的自定義控件訪問此字段。 該控件可以放入任何網站,並且需要檢查該網站的 web.config 以獲得 UninstallSurveySetting 中的 fileLocation 值。
我嘗試了幾種不同的方法,但都沒有運氣。 對此的任何幫助將不勝感激。
更容易使用AppSettings 。
Web.config:
<configuration>
<appSettings>
<add key="fileLocation" value="C:\inetpub\wwwroot\output\" />
</appSettings>
</configuration>
代碼:
string location = System.Configuration.ConfigurationManager.AppSettings["fileLocation"];
如果您的部分將變得更加復雜,那么:
var section = (NameValueFileSectionHandler)ConfigurationManager.GetSection("UninstallSurveySettings");
if (section != null)
{
// access section members
}
附言
也許您想使用ConfigurationSection class 而不是處理程序。
在 ASP.NET MVC 3 中,標簽不能是直接子代(這會導致配置錯誤)。
如何將您的密鑰添加到該部分。 然后,您可以通過ConfigurationManager.AppSettings集合輕松訪問它。
使用System.Configuration.ConfigurationManager
,您將能夠從 web.config 獲得您想要的
我能夠通過為其創建配置 class 並將此代碼放在 web.config 中來解決此問題:
<section name="UninstallSurveyConfig" type="dashboard.UninstallSurveyConfig" />
..
<UninstallSurveyConfig dirFileLocation="C:\inetpub\wwwroot\build\output" webFileLocation="~/output" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.