我想在我的应用程序的 Web.Config 文件中指定的文件路径,然后调用该路径中的控制器。 从什么我已经在网上找到,我大多数的去那儿的路上。
Web.Config
<appSettings>
<add key="filePath" value= "~/App_Data/Physicians.xml" />
</appSettings>
控制器
//Path of the document
string xmlData = ConfigurationManager.AppSettings["filePath"].ToString();
然而,这指向错误的位置。
我如何能指出这到我已经存储在 App_Data 文件夹中,从我的应用程序的根开始的文件?
您可以使用 Server.MapPath
。
或,或者,只有相对的路径存储在配置文件中,然后使用︰
<appSettings>
<add key="filePath" value= "App_Data/Physicians.xml" />
</appSettings>
string relativePath = ConfigurationManager.AppSettings["filePath"];
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, relativePath)
后者的技术将在非 web 应用程序中工作,所以可以说更好。