2010-09-30 202 views
7

有沒有辦法從ASP.NET的URL獲取物理文件路徑? Scenerio:我有一個在兩臺服務器上的應用程序,但現在會有很多應用程序,並且每臺服務器都將它放在不同的物理文件路徑中。現在我這樣做:ASP.NET從URL獲取物理文件路徑

//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\\"; } 

但我需要做的是這樣的:

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

我該怎麼辦呢?

回答

7

您可以在服務器端使用HttpServerUtility.MapPath以獲取文件的物理路徑,然後將其返回到ApplicationSession對象中,與您現在正在執行的操作類似。

至於URL的物理路徑 - 可能沒有一個,因爲URL可以被重寫。

4

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

3

這是現在:

HostingEnvironment.MapPath("/");