2010-09-30 218 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("/");