7

有没有办法从 ASP.NET 的 URL 获取物理文件路径?

Scenerio:我有一个应用程序在两个服务器上,但现在它会在更多服务器上,每个服务器都将它放在不同的物理文件路径中。现在我正在这样做:

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

我该怎么做?

4

3 回答 3

8

您可以HttpServerUtility.MapPath在服务器端使用以获取文件的物理路径,然后在ApplicationorSession对象中返回它,类似于您现在所做的。

至于 URL 的物理路径 - 可能没有,因为 URL 可以重写。

于 2010-09-30T18:38:32.830 回答
4

这个Server.MapPath ( "/" );或这个HttpContext.Current.Server.MapPath ( "/" );应该给你你需要的东西。

于 2010-09-30T18:41:17.397 回答
4

现在是:

HostingEnvironment.MapPath("/");
于 2015-02-24T20:15:43.540 回答