ruud ruud - 2 months ago 5x
ASP.NET (C#) Question

Read data from web.config

How is it possible to read dynamic data from web.config. This is what I have:

<add key="TemplatesRootPath" value="System.Web.Hosting.HostingEnvironment.MapPath('~\\PSDtemplates\\MasterTemplates\\')"/></appSettings>

When I try to get real value of the key TemplatesRootPath:

var result= WebConfigurationManager.AppSettings["TemplatesRootPath"];

I am getting the string under the value tag "System.Web.Hosting.HostingEnvironment.MapPath('~\PSDtemplates\MasterTemplates\)" as a result.

I do not want that, I would like to get something like



The web.config is a XML file you can't use C# code like : System.Web.Hosting.HostingEnvironment because that is not XML.

What you can do however is set "~\PSDtemplates\MasterTemplates\" as your value and then in your code you can say:

string TemplatesRootPath = WebConfigurationManager.AppSettings["TemplatesRootPath"]; TemplatesRootPath = System.Web.Hosting.HostingEnvironment.MapPath("~\PSDtemplates\MasterTemplates\");