2016-08-12 59 views
0

如何从web.config读取动态数据?这是我有:从web.config读取数据

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

当我试图拿到钥匙TemplatesRootPath的真正价值:

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

我所获得的价值标签下的字符串“System.Web.Hosting .HostingEnvironment.MapPath('〜\ PSDtemplates \ MasterTemplates \)“。

我不想这样,我想获得像

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

回答

1

web.config中是不能使用C#代码就像一个XML文件:System.Web.Hosting.HostingEnvironment因为不是XML。

但是你可以做的是设置 “〜\ PSDtemplates \ MasterTemplates \” 作为您的值,然后在你的代码,你可以说:

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

+0

真棒。非常感谢! – ruud