This project has moved. For the latest updates, please go here.

Design time access to web.config

Aug 17, 2010 at 11:28 AM

Hi,

The default method for instantiating a configuration section does not seem to work design-time:

System.Configuration.ConfigurationManager.GetSection("MySectionName")
returns a null-reference when accessed design-time.

I've found some resources on this subject, but they only seem to work for classes that inherit

System.Web.UI.Design.WebControls.ControlDesigner

, and therefore any initialization would not be static nor its behaviour singleton anymore. The 'correct' way to do this would be:

public CustomResourceProvider Instance()
{
	IWebApplication webApp = (IWebApplication)base.Component.Site.GetService(typeof(IWebApplication));
	if (webApp == null) return null;
       	System.Configuration.Configuration config = webApp.OpenWebConfiguration(true);
	return (CustomResourceProvider)config.GetSection("MySectionName");
}

Where "MySectionName" would refer to the constant as defined by CSD.

Any ideas on this?