ruud
ruud

Reputation: 210

Read data from web.config

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

      <appSettings>
<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

C:\\Code\\MyProject\\Project.WEBAPI\\MasterTemplates\\

Upvotes: 0

Views: 126

Answers (1)

Xander Den Hartog
Xander Den Hartog

Reputation: 26

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\");

Upvotes: 1

Related Questions