21

Question Background:

I have a WebApi controller who's logic code relies on reading data contained in a number of XML files. These XML files have been included in the App_Data folder of the WebApi project.

The Issue:

I'm trying to use the relative path of the XML files in the following way:

    [System.Web.Http.HttpGet]
    public string CallerOne()
    {
        string docOne = @"~\AppData\DocOne.xml";

        string poll = @"~\AppData\Poll.xml";

        var response =  _Caller.CallService(docOne, poll);

        return ConvertXmlToJson(response);
    }

When running the WebApi code and calling the Url to the CallerOne method I receive the following error:

An exception of type 'System.IO.DirectoryNotFoundException'
occurred in  System.Xml.dll but was not handled in user code

Additional information: Could not find a part of the path
'C:\Program Files  (x86)\IIS Express\~\AppData\FPS.xml'.

I also want to eventually publish this to Azure and include these files.

How can I use the relative path to read in the XML files in the App_Data folder?

user1352057
  • 3,162
  • 9
  • 51
  • 117

3 Answers3

42

Ended up finding the answer.

The following is needed to read the relative paths in a WebApi project:

var fullPath = System.Web.Hosting.HostingEnvironment.MapPath(@"~/App_Data/yourXmlFile.xml");
user1352057
  • 3,162
  • 9
  • 51
  • 117
  • 1
    How about this : string appData = Environment.GetEnvironmentVariable("AppData"); – jdweng Aug 08 '15 at 04:51
  • @jdweng - But it's not an environment variable, the OP is trying to get to the App_Data folder in his webapi deployment. The answer provided above worked fine, your way produced a path of C:\Windows\System32\config\systemprofile\AppData\Roaming, which isn't what is needed. – dcp Jul 26 '17 at 15:36
  • How do you know what the OP wanted two years ago? – jdweng Jul 26 '17 at 15:54
  • @jdweng, OP clearly stated it in question: "These XML files have been included in the `App_Data` folder of the WebApi project." Usage of `AppData` later in non-working code example should not be considered, and by the way, is simply a typo. – v.karbovnichy Jan 21 '18 at 17:10
7

As jdweng inferred several months back, Environment.GetEnvironmentVariable("AppData") would seem to be the preferred method. The OP's auto-accepted answer and that give quite different results. For example, using both of those in my project, I get:

C:\\Projects\\PlatypusReports\\PlatypusReports\\App_Data\\yourXmlFile.xml

...for the OP's long-winded code, namely this:

var fullPath = System.Web.Hosting.HostingEnvironment.MapPath(@"~/App_Data/yourXmlFile.xml");

...and this:

C:\\Users\\cshannon\\AppData\\Roaming

...for jdweng's code, to wit:

string appData = Environment.GetEnvironmentVariable("AppData");

OTOH, this code:

string appDataFolder = HttpContext.Current.Server.MapPath("~/App_Data/");

returns:

C:\\Projects\\PlatypusReports\\PlatypusReports\App_Data\

So it's very similar in results (if not methodology) to the first example above. I actually got it from a question I asked almost two years ago, which I had forgotten about.

I'm not positive if jdweng's approach would work as expected once the app is deployed on a server, but I have much more confidence in it than the other approaches.

Can anyone verify?

UPDATE

The accepted answer here has 237 upvotes at time of typing, so seems pretty reliable, albeit 6 years old (42 in dog years, which may be a good sign).

Community
  • 1
  • 1
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • 2
    The accepted answer in you update is using HttpContext which you generally wouldn't access in WebApi – heymega Mar 01 '16 at 17:21
  • 1
    @heymega: Would that mean the best method is to use OP's original answer: `HostingEnvironment.MapPath(@"~/App_Data");`? – Simon Elms Jan 08 '17 at 06:49
1

Your approach is fine. You just had some tpying error, You wrote

string docOne = @"~\AppData\DocOne.xml";

But it should have been

string docOne = @"~\App_Data\DocOne.xml";
user1451111
  • 1,735
  • 3
  • 18
  • 30