Sunday, December 28, 2008

How to access web.config from a class project assembly

With VS 2005 it is now easy to access your web.config file from a class within any of your class projects. This is useful for cases wherein your Data Layer needs to access your web.config to retrieve the connectionString section.

public static string GetConnectionStringFromWebConfig()
{
System.Configuration.Configuration cfg =
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
//wfp - open web.config located in the root directory
return cfg.ConnectionStrings.ConnectionStrings[0].ConnectionString;

}

of course plain old ConfigurationManager can still be used.


Share/Save/Bookmark

No comments:

Post a Comment