我被困住了!这看起来真的很愚蠢,但我看不出我哪里出错了。我正在创建一个 2.0 C# ASP.NET 网站。我正在尝试使用 web.config 文件中的自定义部分:
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>