我有一個wbe應用程序mvc c#4.0,我想在運行時從代碼初始化DefaultRoleProvider connectionstring(不希望它來自web.config)。 我創建了一個類MyRoleProvider:DefaultRoleProvider初始化DefaultRoleProvider連接字符串代碼
public class MyRoleProvider : DefaultRoleProvider
{
public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
{
base.Initialize(name, config);
// Update the private connection string field in the base class.
bool IsLocal = bool.Parse(ConfigurationManager.AppSettings["IsLocal"] as string);
string connectionString = "local connectionString ... ";
if (IsLocal == false)
connectionString = "prod connectionString ...";
//ToDo- how to set connection string next property of provider.
??
}
}
也許你應該嘗試動態地設置連接字符串中的配置,並重新加載相應的部分。請參閱http://msdn.microsoft.com/query/dev11.query?appId=Dev11IDEF1&l=EN-US&k=k%28System.Configuration.ConfigurationManager%29;k%28TargetFrameworkMoniker-.NETFramework – jbl 2013-02-21 17:27:30
我無權更改配置文件。 – 2013-02-21 18:34:34