SexyMF
SexyMF

Reputation: 11155

"Could not find a part of the path" c# IIS deployment on production

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?

Upvotes: 1

Views: 8600

Answers (6)

fatnjazzy
fatnjazzy

Reputation: 6152

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

Upvotes: 0

Grant Thomas
Grant Thomas

Reputation: 45083

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.

Upvotes: 0

ptfaulkner
ptfaulkner

Reputation: 712

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"); 

Upvotes: 1

ChrisBD
ChrisBD

Reputation: 9209

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.

Upvotes: 1

user1921
user1921

Reputation:

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";

Upvotes: 0

user
user

Reputation: 6947

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"));

Upvotes: 0

Related Questions