1

I am loading an xml file from my applicatin:

XDocument.Load(HttpContext.Current.Server.MapPath("/") + "XMLMetadata\\Actions.1.xml"); 

In the dev environment it is working fine.

But After I deploy the application, the system cannot find it.
this is the error:
Exception Details: System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\inetpub\wwwroot\XMLMetadata\Actions.1.xml'.

the file was deployed to
C:\inetpub\wwwroot\MyApp\XMLMetadata\Actions.1.xml
and not to:
C:\inetpub\wwwroot\XMLMetadata\Actions.1.xml

ASP .NET 4 MVC APPLICATION What am I missing?

tshepang
  • 12,111
  • 21
  • 91
  • 136
SexyMF
  • 10,657
  • 33
  • 102
  • 206

6 Answers6

1

Hmm - do you mean to ask why it has deployed to C:\inetpub\wwwroot\MyApp\XMLMetadata\ rather than C:\inetpub\wwwroot\XMLMetadata\?

As it is you've answered the reason why you get an error.

It is for this reason that you try and use referential URLs i.e. ~\XMLMetadata\ rather than a hard coded location.

Also ensure that you test on the development system using IIS locally.

ChrisBD
  • 9,104
  • 3
  • 22
  • 35
1

I'm pretty sure you are looking to use the "~" in this case. The "~" used with Server.MapPath will give you the physical path to the root of the application. Where as "/" will give you the physical path to the root of the domain name. These paths can be different based on how you have IIS setup.

XDocument.Load(HttpContext.Current.Server.MapPath("~") + "XMLMetadata\\Actions.1.xml"); 
ptfaulkner
  • 712
  • 4
  • 14
  • This won't work, it will still resolve to the physical path of the root, not the virtual directory of "MyApp". – Grant Thomas Oct 27 '11 at 12:22
  • I think this is still correct as long as his website is marked as an application in IIS. Which I assume is what is going on since it seems that is where his code is running, and the paths are different in development and production. – ptfaulkner Oct 27 '11 at 13:31
0

It's difficult to say what exact framework and base class you are working here, but I'm guessing ASP.NET. If so, you may want to have a look at Control.ResolveUrl().

Then, you should get something like the following instead of your Load() call.

XDocument.Load(this.ResolveUrl("~/XMLMetadata\\Actions.1.xml"));
user
  • 6,897
  • 8
  • 43
  • 79
0

I've used something like this in the past:

    var appPath = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + HttpContext.Current.Request.ApplicationPath;
    if (appPath.Substring(appPath.Length - 1, 1) != "/") appPath = appPath + "/";
    var filePath = appPath + "XMLMetaData//Actions.1.xml";
0

This is because your web application is sat in a virtual directory, so you will need to resolve to that level:

var applicationRoot = 
    HttpContext.Current.Server.MapPath(
        HttpRequest.ApplicationPath);

Remarks on ApplicationPath from MSDN:

Use this property to construct a URL relative to the application root from a page or Web user control that is not in the root directory. This allows pages and shared controls that exist at different levels of a directory structure to use the same code to link to resources at fixed locations in the application.

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
0
XDocument.Load(HttpContext.Current.Server.MapPath("XMLMetadata/Actions.1.xml"));
fatnjazzy
  • 6,070
  • 12
  • 57
  • 83