I want to specify a path to a file in my application in the Web.Config file, then call that path in a controller. From what I've found online, I'm most of the way 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 is pointing to the wrong location.
How can I point this to the file I have stored in the App_Data folder, starting from the root of my application?
You can use Server.MapPath
.
Or alternatively, store only the relative path in the configuration file, then use:
<appSettings>
<add key="filePath" value= "App_Data/Physicians.xml" />
</appSettings>
string relativePath = ConfigurationManager.AppSettings["filePath"];
string combinedPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, relativePath)
The latter technique will work in non-web applications, so is arguably better.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With