我卡住了!這看起來非常愚蠢,但我不明白我要去哪裏錯了。我正在創建一個2.0 C#ASP.NET網站。我想在web.config文件中使用自定義欄目有:C#ConfigurationManager.GetSection無法加載文件或程序集
DatabaseFactorySectionHandler sectionHandler = ConfigurationManager.GetSection("DatabaseFactoryConfiguration") as DatabaseFactorySectionHandler;
我有這在Bailey.DataLayer命名空間中的對象一個單獨的DLL。但是,當我運行Test.aspx的頁面我得到以下錯誤:
System.Configuration.ConfigurationErrorsException was unhandled by user code
Message="An error occurred creating the configuration section handler for DatabaseFactoryConfiguration: Could not load file or assembly 'Bailey.DataLayer' or one of its dependencies. The system cannot find the file specified. (C:\\Documents and Settings\\Administrator.PIP\\My Documents\\Visual Studio 2005\\WebSites\\bailey\\web.config line 13)"
Source="System.Configuration"
,我想獲得的類如下:
namespace Bailey.DataLayer
{
public sealed class DatabaseFactorySectionHandler : ConfigurationSection
{
[ConfigurationProperty("Name")]
public string Name
{
get { return (string)base["Name"]; }
}
[ConfigurationProperty("ConnectionStringName")]
public string ConnectionStringName
{
get { return (string)base["ConnectionStringName"]; }
}
public string ConnectionString
{
get
{
try
{
return ConfigurationManager.ConnectionStrings[ConnectionStringName].ConnectionString;
}
catch (Exception excep)
{
throw new Exception("Connection string " + ConnectionStringName +
" was not found in web.config. " +
excep.Message);
}
}
}
}
}
web配置文件有本節:
<configSections>
<section name="DatabaseFactoryConfiguration"
type="Bailey.DataLayer.DatabaseFactorySectionHandler, Bailey.DataLayer" />
</configSections>
我已經在控制檯應用程序中做了這個沒有問題,但除了它在網頁中看不到任何差異。
編輯
這一切都編譯並在運行時引發錯誤,所以我只能假設它找到組件,因爲它是在test.aspx.cs頁面引用。
我有以下的test.aspx.cs頁面頂部using語句:
using Bailey.DataLayer;
這裏是整個web.config文件,所以沒有混亂:
<configuration>
<configSections>
<section name="DatabaseFactoryConfiguration" type="Bailey.DataLayer.DatabaseFactorySectionHandler, Bailey.DataLayer" />
</configSections>
<appSettings/>
<connectionStrings>
<add name="BaileyMDFConString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\bailey.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient" />
</connectionStrings>
<DatabaseFactoryConfiguration Name="System.Data.SqlClient" ConnectionStringName="BaileyMDFConString" />
<system.web>
<compilation debug="true"/>
<authentication mode="Windows"/>
</system.web>
</configuration>
實際的自定義配置節本身在哪裏? – 2009-01-16 21:08:45
嗯,這是你的問題......你實際上沒有自定義部分。 – 2009-01-16 21:24:40
它就在那裏,但我無法從配置文件中獲取XML以正確顯示,或者現在就完成! – Jon 2009-01-16 21:29:35