John
John

Reputation: 451

Creating a relative path to a file in another project

I'm using this functionality to generate an absolute path to a file

HttpContext.Current.Server.MapPath("/File.jpg");

However it returns the path of a directory in the startup project of the solution. Not the project this code is in and where I want the file to be. When I try go to two steps back and into the other project like this..

HttpContext.Current.Server.MapPath("../../ProjectTwo/File.jpg");

The root directory I get is C:\inetpub\wwwroot\ProjectTwo.

I assume it's because I'm running it on IIS but is there a way to create an absolute path to another project in the solution?

Upvotes: 0

Views: 88

Answers (1)

Albert D. Kallal
Albert D. Kallal

Reputation: 49264

Be it the published web site, or your code, then your addressing to files retrieved by a valid URL means you MUST use Server.Mappath.

So, two solutions exist:

Say you have files on some other "big" server, say a gazillion PDF or image files.

So, then you as a developer have to choose if that other file path is to be exposed to the web side of things, or that you will ONLY allow code behind to retrieve those files. For reasons of security, I thus in most cases do NOT allow valid path names to the file from the web server. Hence, I don't use (or better stated don't allow) such URLs to be valid. If the other path name is only PDFs or pictures that you don't care much about (i.e.: any user can look at and view the files), then you can add what is called a "virtual folder" to the web site. This simply lets you map a path name to the root of the existing web site, and it can be any valid windows path name. So, you might add a virtual folder called:

\\server01\documents\PDF\

And you could give the virtual any name you want, such as PDF.

Hence, then you can use this format:

www.mywebsite.com/PDF/part22.pdf

And thus the above will resolve to

\\server01\documents\PDF\part22.pdf

And Server.Mappath("~/PDF/part22.pdf") will also return the correct windows valid file path.

As noted, often such files belong to one customer and for reasons of security, I don't like mapping internal company folders to valid outside URLs from the web server.

Hence, I tend to "store/save" the external path name in my custom config file (often a database row).

Then to retrieve such files, I use code behind, and "stream" or "send" the file to the client browser for downloading or whatever. This thus enhances security.

Remember, ONLY the web server and valid URLs (including mapped virtual folders) will respect the security setup you have for such folders (used via URLs to the web site).

However, code behind 100% ignores the logged on user, and code behind is free to read/load/copy ANY valid file accessible from your web server (including UNC path names to other servers on the network). So, it's your choice if you want to map out and allow the external web server URLs to map to some internal folder. If not, and for better security, then you ONLY allow code behind to use such files, and never allow the IIS and valid URLs to map to such internal folders.

So, make your decision based on the above:

Do you want users and Server.MapPath from a valid URL to map to the internal files, or do you want ONLY code behind to use such files?

All code behind NEVER uses URLs to read/use files, and such code ALWAYS uses a regular valid windows path names for such code running. As noted, this rule does not apply to existing folders for the web site, or if you added some "virtual" folders to the web site that maps to other folders (including that of folders on other servers/computers on the web server's network).

So, you have to add a virtual folder, or simply store the other path name in some persisting kind of storage such as a database, or config settings.

Upvotes: 0

Related Questions