views:

75

answers:

3

When my ASP.NET site uses documents (e.g. XML), I normally load the document as follows:

Server.MapPath("~\Documents\MyDocument.xml")

However, I would like to move the Documents folder out of the website folder so that it is now a sibling of the website folder. This will make maintaining the documents considerably easier.

However, rewriting the document load code as follows:

Server.MapPath("../../Documents/MyDocument.xml")

results in a complaint from ASP.NET that it cannot 'exit above the top directory'.

So can anyone suggest how I can relatively specify the location of a folder outside the website folder? I really don't want to specify absolute paths for the obvious deployment reasons.

Thanks

David

A: 

If you want to specify the Location somewhere in harddrive , then its is not easily available on web environment. If files are smaller in size and quantity then you can keep it inside directory and point then using ~/path till directory.

But in some cases we used to do Request object. For more visit this link

http://msdn.microsoft.com/en-us/library/5d5940ad.aspx

Amit Ranjan
+2  A: 

If you know where it is relative to your web root you can use Server.Mappath to get the phsical location of your web root and then method on path (http://msdn.microsoft.com/en-us/library/system.io.path_methods.aspx) to get your doument path.

In rough unchecked code something like:

webRootPath = Server.MapPath("~")
docPath = Path.Combine(rootPath, "..\Documents/MyDocument.xml")

Sorry if I got the Syntax wrong but the Path class should be what you are after to play with real FS paths rather than the web type paths.

The reason your method failed is that Server.MapPath takes a location on your web server adn the one you gave is not valid since it is "above" the top of the root of the server hierarchy.

Chris
Yes of course. That looks like the one.
David
Possibly even better is to add the Documents folder as a virtual directory to the website in IIS.
David
You could do that. It really depends if users need access to these documents (or whether you want them to). If there is no need for them ever to be downloaded then there is no real need to have them as a virtual directory. You can of course secure them other ways (deny permissions on the directory, etc.) but not having an associated url is always the best way to secure. I dont' really know what your usage is though so without more idea of what this Documents directory is for I wouldn't like to say what is the best course of action (and its probabyl a separate question anyway). :)
Chris
I see you point about not having a URL. I don't really need to worry too much about that, it's a small intranet and the files are just mail merge templates. I just needed to give one user access to the directory to make copy changes to the documents, without giving her access to the actual website folder.
David
A: 
docPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\Documents\MyDocument.xml");

AppDomain.BaseDirectory returns current web application assembly directory path.

Tadas