I'm writing a server web control that needs to access the web.config file. Specifically, the AppSettings (ConnectionsStrings would be nice to).
I tried using WebConfigurationManager.AppSettings, but it doesn't work in designtime. I had read the ASP.NET 2.0 added the ability to do this, but I can't figure out how.
Also, Is there any equivalent of doing a Server.MapPath() during designtime. My control need access to a configurable scratch folder. At runtime, I use a virtual directory, but at designtime, I can't figure out the physical location of the virtual directory.
Well, I think I answered my own questions. Here's the test code that I put in my custom control. In order for the MapPath one to work on virtual folders that you created in IIS you need to open your project using
http://localhost/myproject, and not use the file system method. This code was tough to figure out. I hope it helps someone.
private String MapPath(string name)
{
if (Page != null && Page.Site != null && Page.Site.DesignMode)
{
IWebApplication app = (IWebApplication)Page.Site.GetService(typeof(IWebApplication));
if (app != null)
{
IProjectItem pItem = app.GetProjectItemFromUrl(name);
if (pItem != null)
{
return pItem.PhysicalPath;
}
else
{
return "URL not found in project";
}
}
return "Can not get IWebApplication at DesignTime";
}
else if (Page != null)//Runtime
{
return Page.MapPath(name);
}
else
{
return "No page to map path from";
}
}
private String GetAppSetting(string name)
{
if (Page != null && Page.Site != null && Page.Site.DesignMode)
{
IWebApplication app = (IWebApplication)Page.Site.GetService(typeof(IWebApplication));
if (app != null)
{
Configuration config = app.OpenWebConfiguration(true);
if (config != null)
{
return config.AppSettings.Settings[name].Value;
}
}
return "Can not get AppSettings at DesignTime";
}
else //Runtime
{
return WebConfigurationManager.AppSettings[name];
}
}
demi
Member
22 Points
8 Posts
Accessing AppSettings and ConnectionStrings from DesignTime
Nov 02, 2005 01:54 PM|LINK
I tried using WebConfigurationManager.AppSettings, but it doesn't work in designtime. I had read the ASP.NET 2.0 added the ability to do this, but I can't figure out how.
Also, Is there any equivalent of doing a Server.MapPath() during designtime. My control need access to a configurable scratch folder. At runtime, I use a virtual directory, but at designtime, I can't figure out the physical location of the virtual directory.
Thanks,
Mark DeMichele
demi
Member
22 Points
8 Posts
Re: Accessing AppSettings and ConnectionStrings from DesignTime
Nov 03, 2005 01:26 PM|LINK
private String MapPath(string name)
{
if (Page != null && Page.Site != null && Page.Site.DesignMode)
{
IWebApplication app = (IWebApplication)Page.Site.GetService(typeof(IWebApplication));
if (app != null)
{
IProjectItem pItem = app.GetProjectItemFromUrl(name);
if (pItem != null)
{
return pItem.PhysicalPath;
}
else
{
return "URL not found in project";
}
}
return "Can not get IWebApplication at DesignTime";
}
else if (Page != null)//Runtime
{
return Page.MapPath(name);
}
else
{
return "No page to map path from";
}
}
private String GetAppSetting(string name)
{
if (Page != null && Page.Site != null && Page.Site.DesignMode)
{
IWebApplication app = (IWebApplication)Page.Site.GetService(typeof(IWebApplication));
if (app != null)
{
Configuration config = app.OpenWebConfiguration(true);
if (config != null)
{
return config.AppSettings.Settings[name].Value;
}
}
return "Can not get AppSettings at DesignTime";
}
else //Runtime
{
return WebConfigurationManager.AppSettings[name];
}
}