简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM