ASP.NET relative path to file in Web.Config

I want to specify the path to the file in my application in the Web.Config file, and then call this path in the controller. From what I found on the Internet, I am most of all there.

Web.config

<appSettings> <add key="filePath" value= "~/App_Data/Physicians.xml" /> </appSettings> 

controller

 //Path of the document string xmlData = ConfigurationManager.AppSettings["filePath"].ToString(); 

However, this indicates an incorrect location.

enter image description here

How can I point this to a file that I saved in the App_Data folder, starting from the root of my application?

+8
c # visual-studio web-config
source share
1 answer

You can use Server.MapPath .

Or, alternatively, save only the relative path in the configuration file, and then use:

 <appSettings> <add key="filePath" value= "App_Data/Physicians.xml" /> </appSettings> string relativePath = ConfigurationManager.AppSettings["filePath"]; Path.Combine(AppDomain.CurrentDomain.BaseDirectory, relativePath) 

The latter method will work in non-web applications, so it might be better.

+18
source share

All Articles