ASP.NET Get physical file path from URL

Is there a way to get the physical file path from an ASP.NET URL?

Scenerio: I have an application that is on two severs, but now it will be much larger, and each server puts it in a different path to the physical file. Now I am doing this:

//for server 1 if (Request.Url.GetLeftPart(UriPartial.Path).Contains(".com")) { Application["StoreFilesPath"] = "E:\\Data\\rootsite\\f1\\appsite\\Upload\\"; } //for server 2 if (Request.Url.GetLeftPart(UriPartial.Path).Contains(".net")) { Application["StoreFilesPath"] = "E:\\Web\\rootsite2\\f34\\abc\\ghi\\appsite\\Upload\\"; } 

But I need to do something like this:

 //for all servers Application["StoreFilesPath"] = getPhysicalFilePath() +"\\Upload\\"; 

How can i do this?

+7
url filepath
source share
3 answers

You can use HttpServerUtility.MapPath on the server side to get the physical path to the file and then return it to the Application or Session object, similar to what you are doing now.

As for the physical path to the URL, there may be more than one, since the URLs can be rewritten.

+7
source share

This Server.MapPath ( "/" ); or this HttpContext.Current.Server.MapPath ( "/" ); should provide you with what you need.

+4
source share

Now this:

 HostingEnvironment.MapPath("/"); 
+3
source share

All Articles